Using variable to create temporary table as another table [duplicate] - sql

This question already has answers here:
T-SQL Dynamic SQL and Temp Tables
(5 answers)
A table name as a variable
(10 answers)
Closed 8 months ago.
I'm building a procedure into SQL Server, and for that I need to receive a table name as a variable, create a temporary table based on the table requested, search some substrings on each column of the given table, remove then, and return the result to the user.
For that, I'm first tried to do that:
declare #TableName varchar(max)
select #TableName = 'xxxxxx'
select * into #temp from #TableName
select * from #temp
From this code, I've got the error:
Must declare the table variable "#TableName"
To use a table variable, I need to know the structure of said table beforehand, which I don't.
If I build the statement and execute it inside an EXEC() function, then the scope changes, and the temporary table will not be available for the outer process.
Is there a way to use the variable directly, or to declare this table variable dynamically?

Related

Run SQL Dynamic using column value [duplicate]

This question already has answers here:
T-SQL: How to use parameters in dynamic SQL?
(4 answers)
sql stored procedure argument as parameter for dynamic query
(2 answers)
SQL Server 2017 - How to pass a parameter in a SELECT inside a dynamic SQL
(1 answer)
Closed last year.
I am using dynamic SQL.
I inserted a select statement into one row from #example, I want to call that select statement and run it with dynamic SQL, my select stored has a variable call #NumOrder
create table #example(id int, description varchar(1000))
insert into #example(id, description)
values(1, 'select case when name=''Carlos'' then ''Pass'' else ''FAIL'' from server.dbo.person where number = #NumOrder')
declare #NumOrder int, #SQL nvarchar(max)
set #NumOrder=2621
set #SQL='description'
--here is where I want to call my query and is failing
--once I filled #NumOrder I want to display the results
exec(#SQL)
I want to run this dynamic SQL using a row stored into the temp table, I know that I can put the complete select statement into the #SQL and works but I want to know if there is a way to call the select statement in a row stored from a table. Is there a way to do this?

return 60 column table from one stored procedure into another [duplicate]

This question already has answers here:
How do I use the results of a stored procedure from within another?
(4 answers)
Closed 3 years ago.
ALTER PROCEDURE [dbo].[outer_sp]
#PrmIdUsAD nchar(128)
,#PrmAnnee smallint
,#PrmMois tinyint
,#PrmIdAct tinyint
AS
BEGIN
DECLARE #DateMin datetime, #DateMax datetime, #idAgence nchar(2), #nomAgence nchar(50);
EXEC inner_sp( #DateMin, #DateMax, #idAgence, #nomAgence)
...
END
inner_sp ends up by (table has 60 columns)
SELECT
ISNULL(nomAgence, '') AS nomAgence,
ISNULL(chemin, '') AS chemin,
...
FROM #temp_arbo_of_4
ORDER BY chemin
END
How do I retrieve this output table in the calling stored procedure ?
I know some variables are not set, but this is outside the scope of question.
I assume you want to do further processing on the results? If not, the output will just be returned anyway.
If you do want to process further, create a temp table with the right columns
CREATE TABLE #temp
(
nomAgence {type},
chemin {type},
...
)
then use select into
SELECT INTO #temp
EXEC SelectOFArbo( #DateMin, #DateMax, #idAgence, #nomAgence);
Note that SQL Server wont let you nest these calls, so SelectOfArbo could not SELECT-INSERT from another proc

Return Table from Store Procedure - SQL Server [duplicate]

This question already has answers here:
SQL server stored procedure return a table
(9 answers)
Closed 4 years ago.
I am trying to get table data which is being returned from Stored procedure.
Create procedure Proc1
as
begin
Select * from Employee
End
Go
I want to use this as :
Select * from Departments D
inner join (Exec proc1) p
on D.Emp_id = p.Emp_id
Please suggest.
Thanks
Short version: you can't. Stored procedures cannot be used as a source of data in a query.
The best you can do is put the results of the stored procedure into a (temporary) table and then query that:
create table #sprocResult (
-- define columns that match the results of the sproc.
-- You should also define a PK, and possibly other indexes
)
insert into #sprocResult exec proc1
(You can use a table valued variable as well.)

Dynamically creating the IN clause in a stored procedure [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Need help in dynamic query with IN Clause
I am using SQL server 2008 and this is the problem that I am facing. I have a table named Cars with a column Company. Now I have a stored procedure that looks something like this
CREATE PROCEDURE FindCars (#CompanyNames varchar(500))
AS
SELECT * FROM Cars WHERE Company IN (#CompanyNames)
I tried something like this and failed
DECLARE #CompanyNames varchar(500)
SET #CompanyNames = '''Ford'',''BMW'''
exec FindCars #CompanyNames
I dont get any rows returned. When I do the following
DECLARE #CompanyNames varchar(500)
SET #CompanyNames = '''Ford'',''BMW'''
Select #CompanyNames
I get the following result
'Ford','BMW'
and if I replace this value in the select statement inside the stored procedure, it works
SELECT * FROM Cars where Company in ('Ford','BMW')
Thus I think that the stored procedure seems to be treating 'Ford','BMW' as one string rather than an array. Could someone please help me with this. How do I dynamically construct the string/array required in the IN clause of the select statement inside the stored procedure.
You are right, you created one string, and that is being processed as a list of strings that contains one string. The commas are just characters in that one string. The only equivilent to an array in SQL Server is a table.
For example; WHERE x IN (SELECT y FROM z).
For this reason many people create a SPLIT_STRING() function that returns a table of items from a given comma delimitted string...
WHERE x IN (SELECT item FROM dbo.split_string(#input_string))
There are many ways to implement that split string. Some return strings, some cast to integers, some accept a second "delimiter" parameter, etc, etc. You can search the internet for SQL SERVER SPLIT STRING and get many results - Including here in StackOverflow.
An alternative is to use dynamic SQL; SQL that writes SQL.
SET #sql = 'SELECT * FROM x WHERE y IN (' + #input_string_list + ')'
SP_EXECEUTESQL #sql
(I recommend SP_EXECUTESQL over just EXEC because the former allows you to use parameterised queries, but the latter does not.)

Save result of stored procedure in a Table variable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to SELECT * INTO [temp table] FROM [stored procedure]
I have a nested stored procedure call
In one of the stored procedures I want to save the result into a table variable likt this :
INSERT INTO #myTable
EXEC sp_myStoredProcedure
however, because the proc. is nested the following error occurs :
An INSERT EXEC statement cannot be nested
The procedure must be called from another procedure, changing this is not an option.
I wanted to try to use an output parameter but it still has to be set with a Insert into statement.
What are other options to save the data that is retrieved from the call of a Stored Procedure into a variable ?
Table variables are not visible to the calling procedure in the case of nested procs. The following is legal with #temp tables.
http://databases.aspfaq.com/database/should-i-use-a-temp-table-or-a-table-variable.html
http://support.microsoft.com/kb/305977/en-us
Not tested but maybe do this:
DECLARE #insertedProc as varchar(300)
SET #insertedProc = 'INSERT INTO ' + #myTable + ' sp_myStoredProcedure'
EXEC #insertedProc
Make sure you have defined what #myTable is before hand.