Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 OPENQUERY & sp_executesql & Parameters

Author  Topic 

OscarLG
Starting Member

2 Posts

Posted - 2012-11-06 : 13:47:03
Hi all,

I'm trying to run a query through a linked server using OPENQUERY and sp_executesql because I need one input parameter and an output paramater:

This is my code:


SET @SQLString = N'SELECT @OUT_Num_regs = COUNT(*) from OPENQUERY(MyLinkServer, ''SELECT dvac_007 FROM cron_007 WHERE codi_007 = @IN_Employee'')'
SET @ParmDefinition = N'@IN_Employee varchar(12), @OUT_Num_regs int OUTPUT'

EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@IN_trabajador = @EMPLOYEE
@OUT_Num_regs = @Num_regs OUTPUT

I get:

OLE DB provider 'MSDASQL' reported an error. The provider did not give any information about the error.

I've been searching this site and I found a lot of good ideas but no one fixed my problem.

Thank you in advance!

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-11-06 : 14:51:17
shouldnt it be this?

SET @SQLString = N'SELECT @OUT_Num_regs = COUNT(*) from OPENQUERY(MyLinkServer, ''SELECT dvac_007 FROM cron_007 WHERE codi_007 = '' + @IN_Employee)'
SET @ParmDefinition = N'@IN_Employee varchar(12), @OUT_Num_regs int OUTPUT'



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

OscarLG
Starting Member

2 Posts

Posted - 2012-11-07 : 07:23:26
Thank you Visakh but unfortunately this didn't fixed it...

I tried this and it worked...

SET @SQLString = N'SELECT @OUT_Num_regs = COUNT(*) from OPENQUERY(MyLinkServer, ''SELECT dvac_007 FROM cron_007 WHERE codi_007 = '
SET @SQLString = @SQLString + N'''''' + @EMPLOYEE + ''''''')'
SET @ParmDefinition = N'@OUT_Num_regs int OUTPUT'
EXECUTE sp_executesql
@SQLString,
@ParmDefinition,
@OUT_Num_regs = @Num_regs OUTPUT

Don't ask me to explain it 'cause I don't know how :-(
It was just adding "'" before and after the @EMPLOYEE variable.

Best regards / Un saludo
Oscar L.
Go to Top of Page
   

- Advertisement -