SQL: Dynamisk select m.m..

1 post / 0 new
benzzon
benzzon's picture
SQL: Dynamisk select m.m..

/* *** Skapar & fyller tabell med data från angiven vy.. */


declare @view_name nvarchar(255) declare @SQL nvarchar(1024)

select @view_name='namn på vyn här'

select @SQL='select * into ' + @view_name + '_table from ' + @view_name

exec sp_executesql @statement=@SQL go

------------------------------------- -- setting a variable from dynamic sql Author Nigel Rivett

 

declare @i int
exec sp_executesql N'select @i = 999', N'@i int output', @i output
select @i


-- setting output parameter from dynamic stored procedure call

declare @OutputParameter varchar(100) ,
        @error int ,
        @SPName varchar(128) ,


        @SPCall nvarchar(128) ,
        @rc int
select @SPCall = 'exec ' + @SPName + ' @OutputParameter output'
exec @rc = sp_executesql @SPCall, N'@OutputParameter varchar(100) output', @OutputParameter output
select @Error = @@error

--------------------------------------------

"The curse & blessings of dynamic SQL": http://www.sommarskog.se/dynamic_sql.html#sp_executesql