SQL Server Performance Forum – Threads Archive
How can I return output param. with Dynamic Sql?
I want to return @TotalRegisters parameter to web app, but I don’ t know how to do it. I tried this: (But it doesn#%92 t work, I receive always @TotalParameters = Null)USE market
GO
ALTER PROCEDURE test_dyn2
@TotalRegisters nvarchar(100) output
As BEGIN
SET NOCOUNT ON
Declare @sql nvarchar(4000), @param_list nvarchar(4000) SELECT @sql =
‘ SELECT ‘ + @TotalRegisters + ‘ = COUNT(*)
FROM Offers’
SELECT @param_list = ‘@TotalRegisters nvarchar(100) output’ EXEC sp_executesql @sql, @param_list, @TotalRegisters output
SET NOCOUNT OFF
END
GO
Thank you,
Cesar
Finally I figured it out:
USE market
GO
ALTER PROCEDURE test_dyn2
@TotalRegisters nvarchar(100) output
As
BEGIN
SET NOCOUNT ON
Declare @sql nvarchar(4000),
@param_list nvarchar(4000) SELECT @sql = ‘ SELECT @TotalRegisters = COUNT(*) FROM Offers’
SELECT @param_list = ‘@TotalRegisters nvarchar(100) output’ EXEC sp_executesql @sql, @param_list, @TotalRegisters output SET NOCOUNT OFF
END
GO
<br />Yes! Now it works [<img src=’/community/emoticons/emotion-1.gif’ alt=’

May I ask you before looking at your code, why are you using dinamic sql for that? I don’t see anything that can’t be done with normal sql.
It doesn#%92 t matter, thank you for your aid in my first question!
quote: why are you using dinamic sql for that? I don’t see anything that can’t be done with normal sql.
That was only a little example of my complete query, if you want to see why I may need Dynamic Sql look at this recent posthttp://www.sql-server-performance.com/forum/topic.asp?TOPIC_ID=10977 . This is only a third part or less of complete ‘Where#%92 clause I need. The user can search offers specifying whatever combination among 30 parameters, and this can be complicated to handle with static Sql…
Also, look at this articlehttp://www.sommarskog.se/dyn-search.html .
Those are the main reasons why I am trying to write my code with Dynamic Sql I’ m sorry, the post I pointed to was:
http://www.sql-server-performance.com/forum/topic.asp?TOPIC_ID=10829
quote:
SELECT @sql = ‘ SELECT convert(char(23), @offerDate, 126) = offe.offerDate, @Id = offe.Offer_id
From Offers As offe
WHERE 1 = 1′
You can’t assign a value to an expression. You have to assign to variable. Why do you need that conversion?
SELECT @sql = ‘ SELECT convert(char(23), @offerDate, 126) = offe.offerDate, @Id = offe.Offer_id
From Offers As offe
WHERE 1 = 1′
You are right, now it works! And I have all the pieces of the puzzle [<img src=’/community/emoticons/emotion-1.gif’ alt=’

]]>