Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic strings & variables

Status
Not open for further replies.

chenthorn

Programmer
May 16, 2002
18
US
Why can I not pass the results of a dynamic string query straight into a variable?
I have this query to find the max uid for a givern job run. I paramaterized the server name so that the table can be moved between servers for disaster recovery:
declare @destsvr varchar(30),
@file varchar(30),
@cmd varchar(300),
@marker Int

select @destsvr = '[PRD-005].Operations.DBO'
select @file = 'test1.txt'

select @cmd = ('select MAX(UID) FROM '+@destsvr+'.ProcessStatus Where SubApp = @File')
exec @cmd

This query qill return a UID that I want to assign to @Marker, thus allowing me to do updates later on in the process.
Other than inserting the results to a table, then assigning that value to my marker var, does anyone see any other way to get this uid out of the dynamic string and into a variable?
 
A few small syntax changes will do the trick...
Code:
declare @destsvr    varchar(30),
    @file        varchar(30),
    @cmd        varchar(300),
    @marker        Int

select @destsvr = '[PRD-005].[Operations].[DBO]'
select @file = 'test1.txt'

select @cmd = 'select MAX(UID) FROM '+@destsvr+'.ProcessStatus Where SubApp = @File'
exec (@cmd)
HTH, MapMan [americanflag]

Assume nothing, question everything, be explicit not implicit, and you'll always be covered.
 
Oops,

Let's try that again...

Code:
declare @destsvr    varchar(30),
    @file        varchar(30),
    @cmd        varchar(300),
    @marker        Int

select @destsvr = '[PRD-005].[Operations].[DBO]'
select @file = 'test1.txt'

select @cmd = 'select MAX(UID) FROM '+@destsvr+'.ProcessStatus Where SubApp =
' +
Code:
@File
exec (@cmd)
HTH,

MapMan MapMan [americanflag]

Assume nothing, question everything, be explicit not implicit, and you'll always be covered.
 
Honest, this time it's right...

The four apostrophes in a row put the variable (@File) in the correct context in order to pass it to the string that's called by the execute (Exec) command.

I apologise for posting incorrect versions twice. [blush]
Code:
declare @destsvr    varchar(30),
        @file       varchar(30),
        @cmd        varchar(300),
        @marker     Int

select  @destsvr =  '[PRD-005].[Operations].[DBO]'
select  @file    =  'test1.txt'

select  @cmd     =  'select MAX(UID) FROM ' + @destsvr 
select  @cmd     =  @cmd + '.ProcessStatus Where SubApp = '
select  @cmd     =  @cmd + '''' + @FILE + ''''
exec (@cmd)
HTH, MapMan [americanflag]

Assume nothing, question everything, be explicit not implicit, and you'll always be covered.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top