Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Expand a variable in line?
Message
From
02/01/2001 10:00:58
 
 
To
27/12/2000 15:30:29
General information
Forum:
Microsoft SQL Server
Category:
SQL syntax
Miscellaneous
Thread ID:
00457150
Message ID:
00458441
Views:
9
Try something like this:
declare
@database varchar(32)
,@field char(20)
,@sql varchar(1000)

create table #Results (field1 as char(20))

set @database = 'mydatabase'

set @sql = 'insert #Results select myfield from '+@database+'.dbo.sometable'

exec( @sql )

select @field = myfield from #Results
BOb


>is there any way to expand a variable in line in a select statement without having to do the whole dang thing in dynamic sql? what I would like is:
>
>
declare @database varchar(32), @field char(20)
>set @database = 'mydatabase'
>
>select @field = myfield from @database .dbo.sometable
>
>but of course- this doesn't work - I can do it in dynamic SQL, but then the var @field isn't avaialble in the calling procedure.
>
>Is there any way just to imbed the variable in the select statement or otherwise pull this off without having to go to temp tables?
>
>Thanks,
>Ken
Previous
Reply
Map
View

Click here to load this message in the networking platform