I have a stored Procedure (Generate_Insert)which will output an Insert statement as output given a table name.
But Now I have created another procedure which looks like:
Create Procedure Inserts
As
Begin
EXEC Generate_Insert #Table = 'Admin'
EXEC Generate_Insert #Table = 'Impas'
EXEC Generate_Insert #Table = 'Asui'
EXEC Generate_Insert #Table = 'Alstd'
END
The sample output of
EXEC Generate_Insert #Table = 'Admin' is:
Insert into Admin(Ad_ID,Name,Desc) Values (1,'John','Employee')
The problem is when I execute this procedure I am getting result sets in different windows but i want the output as one result set.
How can I do this?
Assuming the output of Generate_Insert is a varchar(max)
you can do this inside Inserts:
create table #temp
(
insert_stmt varchar(max)
)
insert into #temp
EXEC Generate_Insert #Table = 'Admin'
insert into #temp
EXEC Generate_Insert #Table = 'Impas'
insert into #temp
EXEC Generate_Insert #Table = 'Asui'
insert into #temp
EXEC Generate_Insert #Table = 'Alstd'
select * from #temp
Related
Is there a way to have a statement of a kind:
SELECT <table>.ID, <table>.Parameter
FROM <table> t
JOIN <tableValuedFunction> (<table>.Parameter) tt ON t.ID = tt.ID
I think what you are after is a query that uses cross apply.
SELECT <table>.ID, <table>.Parameter
FROM <table> t
CROSS APPLY <tableValuedFunction> (<table>.Parameter)
In Sql Server, use EXEC with dinamic query:
Declare #sSql Varchar(max)
Declare #table VArchar(25) = 'table'
declare #colum VArchar(25) = 'column'
SET #sSql = 'SELECT table.ID, table.Parameter
FROM table t
JOIN tableValuedFunction ('''+#table+'.'+#colum+''') tt ON t.ID = tt.ID'
EXEC (#sSql)
I need help with my stored procedure which I'm currently working on. Basically stored procedure works fine and gets me required data. I would like to keep this funcionality and add new temporary table within stored procedure and populate this temp table with the data that I get.
I don't know where/and how should I use INSERT INTO SELECT statement or SELECT INTO in my particular stored procedure.
Below I'm submiting my symplified stored procedure :
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_GetHourReportData]
#dateFrom SMALLDATETIME,
#dateTo SMALLDATETIME,
#hourFrom INT,
#hourTo INT
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE tempTable
(
-- fields
)
IF (DATEDIFF(DAY, #dateFrom, #dateTo) = 0)
BEGIN
SELECT -- fields
FROM -- tables
WHERE -- conditions
END
ELSE IF (DATEDIFF(DAY, #dateFrom, #dateTo) = 1)
BEGIN
SELECT -- fields
FROM -- tables
WHERE -- conditions
UNION ALL
SELECT -- fields
FROM -- tables
WHERE -- conditions
END
ELSE
BEGIN
SELECT -- fields
FROM -- tables
WHERE -- conditions
UNION ALL
SELECT -- fields
FROM -- tables
WHERE -- conditions
UNION ALL
SELECT -- fields
FROM -- tables
WHERE -- conditions
END
END
Thanks. Any help will be appreciated.
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[sp_GetHourReportData]
#dateFrom SMALLDATETIME,
#dateTo SMALLDATETIME,
#hourFrom INT,
#hourTo INT
AS
BEGIN
SET NOCOUNT ON;
CREATE TABLE #tempTable --notice the #
(
-- fields
)
IF (DATEDIFF(DAY, #dateFrom, #dateTo) = 0)
BEGIN
insert into #tempTable
SELECT -- fields
FROM -- tables
WHERE -- conditions
END
ELSE IF (DATEDIFF(DAY, #dateFrom, #dateTo) = 1)
BEGIN
insert into #tempTable
SELECT -- fields
FROM -- tables
WHERE -- conditions
UNION ALL
SELECT -- fields
FROM -- tables
WHERE -- conditions
END
ELSE
BEGIN
insert into #tempTable
SELECT -- fields
FROM -- tables
WHERE -- conditions
UNION ALL
SELECT -- fields
FROM -- tables
WHERE -- conditions
UNION ALL
SELECT -- fields
FROM -- tables
WHERE -- conditions
END
END
CREATE TABLE tempTable
(
-- fields
)
IF (DATEDIFF(DAY, #dateFrom, #dateTo) = 0)
BEGIN
INSERT INTO tempTable
(SELECT -- fields
FROM -- tables
WHERE -- conditions)
END
ELSE IF (DATEDIFF(DAY, #dateFrom, #dateTo) = 1)
BEGIN
INSERT INTO tempTable
(SELECT -- fields
FROM -- tables
WHERE -- conditions
UNION ALL
SELECT -- fields
FROM -- tables
WHERE -- conditions)
END
ELSE
BEGIN
INSERT INTO tempTable
(SELECT -- fields
FROM -- tables
WHERE -- conditions
UNION ALL
SELECT -- fields
FROM -- tables
WHERE -- conditions
UNION ALL
SELECT -- fields
FROM -- tables
WHERE -- conditions)
END
END
select * from tempTable
How can i get list of databases?
Select database if have table "test_table".
I don't now how to set condition in
SELECT * FROM master.dbo.sysdatabases
You can build a dynamic query that checks sys.tables in each database.
declare #S1 nvarchar(max)
declare #S2 nvarchar(max)
set #S2 = ' union all select ''[DBNAME]'' from [DBNAME].sys.tables where name = ''test_table'''
select #S1 = stuff((select replace(#S2, '[DBNAME]', quotename(name))
from master.dbo.sysdatabases
for xml path('')), 1, 11, '')
exec (#S1)
How can I use a table variable while executing a command string?
DECLARE #FileIDs TABLE
(
File_ID int
)
insert into #FileIDs select ID from Files where Name like '%bla%';
DECLARE #testquery as varchar(max);
set #testquery = 'select * from #FileIDs';
exec(#testquery);
returns the following error
Msg 1087, Level 15, State 2, Line 1
Must declare the table variable "#FileIDs".
The table #FileIDs is not in the scope of exec(#testquery).
That's why you have that problem.
To solve this problem you may use a temporary table:
CREATE table #FileIDs
(
File_ID int
)
insert into #FileIDs select ID from Files where Name like '%bla%';
DECLARE #testquery as varchar(max);
set #testquery = 'select * from #FileIDs';
exec(#testquery);
drop table #FileIDs
or put the table in the scope:
DECLARE #sql nvarchar(2000)
SET #sql='DECLARE #FileIDs TABLE ( File_ID int);'
SET #sql=#sql+'insert into #FileIDs select ID from Files where Name like ''%bla%'';'
set #sql=#sql+ 'select * from #FileIDs;'
EXECUTE sp_executesql #sql
Indeed table is out of the scope, try this:
DECLARE #sql nvarchar(2000)
SET #sql='DECLARE #FileIDs TABLE ( File_ID int);'
SET #sql=#sql+'insert into #FileIDs select ID from Files where Name like ''%bla%'';'
set #sql=#sql+ 'select * from #FileIDs;'
EXECUTE sp_executesql #sql
CREATE table #FileIDs
(
File_ID int
)
insert into #FileIDs select ID from Files where Name like '%bla%';
DECLARE #testquery as varchar(max);
set #testquery = 'select * from #FileIDs';
exec(#testquery);
drop table #FileIDs
I have the following code:
DECLARE #temp_table_1 TABLE (id int identity(0, 1), col_1 varchar(50)),
#txtVar VARCHAR(MAX)
INSERT INTO #temp_table_1
SELECT col_1 FROM table_1 -- This table_1 is a real table in the database.
Set #txtVar = 'SELECT * FROM #temp_table_1'
EXECUTE (#txtVar)
The error I get is
Declare variable #temp_table_1.
How can I fix this?
Set #txtVar = 'SELECT * FROM myTable WHERE column_value=''' + #var1 + ''''
This article will help you get a basic ideas of dynamic sql.
EDIT
It is not possible to use table variables in a dynamic query.
You have to use temporary table or Use custom TABLE type.
Temporary table
CREATE TABLE #temp_table_1
(
id INT IDENTITY(0, 1),
col_1 VARCHAR(50)
)
DECLARE #txtVar VARCHAR(MAX)
INSERT INTO #temp_table_1
SELECT col_1
FROM table_1 -- This table_1 is a real table in the database.
SET #txtVar = 'SELECT * FROM #temp_table_1'
EXECUTE (#txtVar)
DROP TABLE #temp_table_1
Custom Table Type
CREATE TYPE DefaultTable AS TABLE (ID INT IDENTITY(0, 1), COL_1 VARCHAR(50))
GO
-- Fill a var of that type with some test data
DECLARE #MyTable DefaultTable
INSERT #MyTable
SELECT col_1 FROM table_1 -- This table_1 is a real table in the database.
-- Now this is how you pass that var into dynamic statement
EXECUTE sp_executesql N'SELECT * FROM #MyTable',
N'#MyTable DefaultTable READONLY',
#MyTable