-2
I need to make a Stored Procedure of the following kind:
USE master
GO
CREATE PROCEDURE dbo.sp_DR @bd NVARCHAR(20), @VAL AS INT
AS
SELECT * INTO #TEMP FROM @bd where campo =@val
SELECT campo1, campo2 from #temp
Em que @bd seria a base de dados e a tabela que iria utilizar.
How to pass this indication to the Stored Procedure?
If you put the BD table manually the SP is working as intended.
Thanks Ricardo. had already tried a similar approach, without success. I get the message: " (564 Rows affected) Msg 208, Level 16, State 0, Procedure dbo.sp_dr, Line 9 [Batch Start Line 0] Invalid Object name '#TEMP'. "
– Nuno Morgado
I took your tip with the following change: inside SP I created a table (#TEMP) with the same structure as the original, and in the command I used 'INSERT INTO #temp SELECT * FROM ' + @bd + ' WHERE field = ' + STR(@val). at the end DROP TABLE #temp
– Nuno Morgado
I think the error may be because, as it is a temporary table, it has been created in some context the part within the
exec
, which makes her not visible outside theexec
, for being temporary can for example put theselect
also within theexec
and take a test. Another change would be to create the table as "global Temporary", ie with ##TEMP. The downside is that since it’s global, it can conflict with another user, so it needed to generate a unique name before creating and preferably make adrop table
at the end– Ricardo Pontual