all groups > sql server programming > november 2005 >
You're in the

sql server programming

group:

How to Using Variable in OPENROWSET T-SQL


How to Using Variable in OPENROWSET T-SQL Resant
11/8/2005 9:07:07 PM
sql server programming:
Hi,

How to use variable as parameter in stored procedure using OPENROWSET
??
I try the query below, but get an error. Please help me.

DECLARE @job varchar(5)
SET @job='RefreshTLMReport'

SELECT *
FROM
OPENROWSET('sqloledb'
, 'server=reportsvr;trusted_connection=yes'
, 'set fmtonly off exec msdb..sp_help_job @job_name='
+ @job + ')'


Thanks
Re: How to Using Variable in OPENROWSET T-SQL Uri Dimant
11/9/2005 12:00:00 AM
Resant


declare @par int,@sql varchar(8000)
set @par=1
set @sql ='exec mysp ' + cast(@par as varchar(10))+''''


EXEC ('select *
from
OPENROWSET(''SQLOLEDB'',''SERVER=name;DATABASE=pubs;UID=sa;PWD=pass;'',''set
fmtonly off; ' + @sql+')')





[quoted text, click to view]

RE: How to Using Variable in OPENROWSET T-SQL John Bell
11/9/2005 12:16:01 AM
Hi

sp_help_job will not return a single resultset therefore you can't use it in
OPENQUERY. Your code is also truncating the jobname to 5 characters. Try

DECLARE @job sysname
SET @job='RefreshTLMReport'
EXEC reportsvr.msdb..sp_help_job @job_name=@job

John

[quoted text, click to view]
Re: How to Using Variable in OPENROWSET T-SQL Resant
11/9/2005 7:19:08 PM
I think it's much better, but thanks all.

DECLARE @job varchar(5)
SET @job='RefreshTLMReport'

SELECT *
FROM
OPENROWSET('sqloledb'
, 'server=reportsvr;trusted_connection=yes'
, 'set fmtonly off exec msdb..sp_help_job')
WHERE name=@job
Re: How to Using Variable in OPENROWSET T-SQL John Bell
11/10/2005 12:03:01 AM
Hi Resant

This will produce different results to running it directly and specifying
the @job_name!

You have still declared @job as varchar(5) instead of a sysname.

John

[quoted text, click to view]
AddThis Social Bookmark Button