Sunday, February 12, 2012

Concatination problem

Hi all,

I need to get in my stored procedure name of 'result' field as parameter.
I'm trying to implement that using sp_sqlexec but everytime I get error...
Any ideas?
---------------------------
Create procedure MyProc
@.CurClientID varchar(10)
@.CurCounterName varchar(20)

AS

declare @.ExecString varchar(300)

set @.ExecString = 'Select ' + @.CurCounterName +
' FROM Client WHERE ClientID=' + @.CurClientID

exec sp_sqlexec @.ExecStringOriginally posted by yurich
Hi all,

I need to get in my stored procedure name of 'result' field as parameter.
I'm trying to implement that using sp_sqlexec but everytime I get error...
Any ideas?
---------------------------

this should work :

Create procedure MyProc
@.CurClientID varchar(10)
@.CurCounterName varchar(20)

AS

declare @.ExecString varchar(300)

set @.ExecString = 'Select ' + @.CurCounterName +
' FROM Client WHERE ClientID=' + @.CurClientID

exec sp_sqlexec @.ExecString

Create procedure MyProc
@.CurClientID varchar(10)
@.CurCounterName varchar(20)

AS

declare @.ExecString varchar(300)

set @.ExecString = 'Select ' + @.CurCounterName +
' FROM Client WHERE ClientID=' + @.CurClientID

exec (@.ExecString)|||The holy book[SQL Server Books online] says so :

sp_sqlexec provided a convenient way for SQL Server database clients and servers to send a language statement of any format to an Open Data Services server application. Removed; no longer available. Remove all references to sp_sqlexec.|||It does not help...|||What is the error you are getting ?|||Syntax error converting the varchar value ...|||Originally posted by yurich
Hi all,

I need to get in my stored procedure name of 'result' field as parameter.
I'm trying to implement that using sp_sqlexec but everytime I get error...
Any ideas?
---------------------------
Create procedure MyProc
@.CurClientID varchar(10),-- just add comma !!!!
@.CurCounterName varchar(20)

AS

declare @.ExecString varchar(300)

set @.ExecString = 'Select ' + @.CurCounterName +
' FROM Client WHERE ClientID=' + @.CurClientID

--exec sp_sqlexec @.ExecString
exec(@.ExecString)|||Snail you beat me to it ,

Yurich ...thats the only error i could find in the code .. rest the code seems fine|||I imagine your missing the quotes...

DECLARE @.SQL varvhar(8000)
SELECT @.SQL = 'Select ' + @.CurCounterName
+ ' FROM Client WHERE ClientID=' + '''' + @.CurClientID + ''''
SELECT @.SQL

exec (@.ExecString)|||Originally posted by Brett Kaiser
I imagine your missing the quotes...

DECLARE @.SQL varvhar(8000)
SELECT @.SQL = 'Select ' + @.CurCounterName
+ ' FROM Client WHERE ClientID=' + '''' + @.CurClientID + ''''
SELECT @.SQL

exec (@.ExecString)


It works! Thanks a lot. Now I need to get out result of this query to local variable:

DECLARE @.SQL varvhar(8000)
DECLARE @.Res varchar(100)
SELECT @.SQL = 'Select [@.Res] ' + @.CurCounterName
+ ' FROM Client WHERE ClientID=' + '''' + @.CurClientID + ''''

exec (@.ExecString)
It works, but after executing @.Res = NULL...|||Originally posted by yurich
It works! Thanks a lot. Now I need to get out result of this query to local variable:

DECLARE @.SQL varvhar(8000)
DECLARE @.Res varchar(100)
SELECT @.SQL = 'Select [@.Res] ' + @.CurCounterName
+ ' FROM Client WHERE ClientID=' + '''' + @.CurClientID + ''''

exec (@.ExecString)
It works, but after executing @.Res = NULL...

You cannot save result to local variable but you could save it to temporary table:

create table #tmp(...)
insert #tmp
exec(...)

No comments:

Post a Comment