last day (18 days later) » 

09:42
2
A: Creating temporary table dynamically using condition

Mr.BrownstoneYou are thinking about the problem the wrong way. You can use dynamic SQL to do this - by defining your result shape within the statement, but creating the temporary table outside of it. Like so: CREATE PROC test @var1 CHAR(1) as BEGIN -- USING dynamic sql DECLARE @sql VARCHAR(MAX) IF(@var1 =...

this doesn't create the #result table dynamically right, we have to declare it beforehand.
No, you don't need to declare it beforehand, it'll get created when the statement executes.
I did test your code and it's throwing the error. Am I missing something?
What error is it throwing?
,Msg 208, Level 16, State 0, Procedure dbo.test, Line 67 Invalid object name '#result'.
09:42
Thanks, updated my answer for you.
hey
we are still back on the same page
Hi
How so?
ALTER PROC test @var1 CHAR(1)
as
BEGIN
-- USING dynamic sql
DECLARE @sql VARCHAR(MAX)
IF(@var1 = 'X')
BEGIN
SELECT *
INTO #results
FROM OPENROWSET(
'SQLNCLI',
'Server=(local);Trusted_Connection=yes;', -- replace with actual server/instance
'SELECT * FROM sys.tables'); -- replace with query
END
IF(@var1 = 'Y')
BEGIN
SELECT *
INTO #results
FROM OPENROWSET(
'SQLNCLI',
'Server=(local);Trusted_Connection=yes;', -- replace with actual server/instance
'SELECT * FROM sys.tables'); -- replace with query
the script will be somthing like this right
the SELECT *
INTO #results is not allowed
having a create table multiple times
my workaround was to create a global temp table
but that have an issue of concurrency
You are correct. Which means that what you want to achieve cannot be done. You must either use a global temporary table or refactor your code to work around the restrictions placed upon you by the system.
One moment, I may have an idea
No, what I wanted to do wont work either.
10:11
yeah
anyway thanks
for helping out

  last day (18 days later) »