June 17, 2021 at 2:31 pm
I have created table variable as below , just want to that result into table as last line is not working please help on that,
Select * into table_temp from execute (@query) how to make sure this query like select * into table_2 from table_1
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX)
select @cols = STUFF((SELECT distinct ', ' + QUOTENAME(Finding+'_'+ltrim(rtrim(Units)))
from Vitals2Y
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
--Select @cols
set @query = 'SELECT PatientID,
LastName,
FirstName,
MRN,
DOB,
SSN,
SexDE,
EncounterDTTM,
PerformedDTTM,
ProviderCode,
ProviderLastName,
ProviderFirstName,
ProviderNPI, ' + @cols + '
FROM
(
select PatientID,
LastName,
FirstName,
MRN,
DOB,
SSN,
SexDE,
EncounterDTTM,
PerformedDTTM,
ProviderCode,
ProviderLastName,
ProviderFirstName,
ProviderNPI, Finding+''_''+ltrim(rtrim(Units)) as Finding, Value
from Vitals2Y --where PatientID=''1226738''
) x
pivot
(
min(Value)
for Finding in (' + @cols + ')
) p
ORDER BY PatientID
'
execute (@query)
Select * into table_temp from execute (@query)
June 17, 2021 at 5:19 pm
Change your dynamic SELECT into a dynamic INSERT:
/* instead of: */
set @query = 'SELECT PatientID, LastName, FirstName, .....'
/* use: */
set @query = '
INSERT temp_table(PatientID, LastName,FirstName, ......
SELECT PatientID, LastName, FirstName, ..... '
Eddie Wuerch
MCM: SQL
June 18, 2021 at 3:35 pm
Thank you for you reply, but columns will dynamic from pivot.. So I would like to create table like select * into table_bak from table
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply
This website stores cookies on your computer.
These cookies are used to improve your website experience and provide more personalized services to you, both on this website and through other media.
To find out more about the cookies we use, see our Privacy Policy