I have a project in which I need to save the return value of a stored procedure.
declare @query_bereich nvarchar(500)
SET @query_bereich = 'select bereich' + convert(varchar(2), @bereich) +
' from ImpArtGruppe where Artikelnummer = ' + @artnum
set @artgrp = (EXEC sp_executesql @query_bereich)
select @artgrp = ISNULL(@artgrp, '')
select @artgrp
Now the problem I’m having is, that the return value is always 0. I guess this is by default because in another scenario the would be a restulset that can have more than one value. But in my case I have made sure the the query always returns only the one.
I have also tried foillowing code I found on google:
EXEC @artgrp = sp_executesql @query_bereich
But this did also not work.
Can anyone help me with this?
There’s no stored procedure in your example code. It looks like you are building a dynamic T-SQL query, and want to store the result from that.
Here’s one way, using
sp_executesql, and assuming the type of@artgrpisvarchar(50):