SQL Server : Insert two stored procedure results into one table - sql

I have two stored procedure sp1 and sp2
sp1 returns results
value1
------
1
2
3
4
5
sp2 returns results
value2
------
4
5
6
7
8
I have a table called test that has two columns value1 and value2, how to insert sp1 result in value1 column and sp2 result in value2 column in test table?
I am using this
insert into test
exec [sp1], exec [sp2]
It is causing an error but it is working for single value please click following link

The only way I can think of is the following:
declare #t1 as table (id int identity(1,1), val int);
declare #t2 as table (id int identity(1,1), val int);
insert into #t1 (val)
exec sp1;
insert into #t2 (val)
exec sp2;
insert into test
select t1.val, t2.val
from #t1 t1 full outer join
#t2 t2
on t1.id = t2.id

If these are functions you can do the following, pseudocode:
first_val = select sp1();
second_val = select sp2();
insert into test values (first_val,second_val);
This may work as well:
insert into test values (select sp1(),select sp2());
You cannot use procedures as there is no way to return a value from a procedure.

Related

Refer to previous computed column in SQL

I am trying to calculate the val column using id column and the previous val column. On Excel, I simply use the formula "=IF(B2=1,1,C1+B2)". How can i reference previous value of my column being computed?
id val
1 1
2 3
3 6
4 10
5 15
6 21
7 28
8 36
9 45
10 55
My query would look like
SELECT id,
Case
When id =1 then 1
Else id+*previousval*
end as val
from
tab
It is better if you can tag your DBMS and its version..
Assuming that you are using SQL Server:
SQL Server BELOW 2012 (you can use the Common Table Expression - CTE)
CREATE TABLE #TEST(ID INT)
INSERT INTO #TEST VALUES
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)
;WITH CTE
as
(
SELECT ID, ID AS VALUE FROM #TEST WHERE ID=1
UNION ALL
SELECT T.ID, T.ID + C.VALUE
FROM #TEST T INNER JOIN CTE C ON T.ID = C.ID+1
)
SELECT * FROM CTE
DROP TABLE #TEST
SQL Fiddle for version 2012 Below
SQL Server 2012: (you can use OVER clause)
CREATE TABLE #Test(ID INT)
INSERT INTO #Test VALUES
(1),(2),(3),(4),(5),(6),(7),(8),(9),(10)
SELECT id, SUM(id) OVER(ORDER BY id) AS [Value] FROM #Test ORDER BY id
DROP TABLE #Test
SQL Fiddle: SQL Server 2012
You can try this solution for SQL Server (using variable will give you a best performance):
DECLARE #TempTable TABLE (Id INT, Val INT)
INSERT INTO #TempTable VALUES(1,0)
INSERT INTO #TempTable VALUES(2,0)
INSERT INTO #TempTable VALUES(3,0)
INSERT INTO #TempTable VALUES(4,0)
INSERT INTO #TempTable VALUES(5,0)
INSERT INTO #TempTable VALUES(6,0)
INSERT INTO #TempTable VALUES(7,0)
DECLARE #PrevSum INT = 0
UPDATE #TempTable
SET #PrevSum = Val = #PrevSum + Id
FROM #TempTable
SELECT * FROM #TempTable

T-SQL Insert Column from Other Table

Hello I am trying to insert values from one table column to another table. I am using SQL Server 2008 R2. Here is example:
Table 1
Declare #Table1 table (file varchar(15), type int, partiesid int)
Table 2
Declare #Table2 table (file varchar(15), ....many other columns)
I would like to insert file from Table 2 into Table 1 as well as some other static values.
So select * from table1 would end up looking like this:
File Type PartiesID
GW100 1 555
GW101 1 555
GW103 1 555
GW104 1 555
where the GW100, GW101, etc come from table2 and the 1 and 555 are static for every row.
I tried insert into table1 (file,type,partiesid) values (select file from table2,1,555) but that doesn't work. Is there a way where it will just insert a row for each unique file and also insert the static fields of 1 and 555 as separate columns?
Thanks!
Insert into #table2
(
file,
type,
partiesid,
...(other columns for which you need to give static values)
)
select
file,
type,
partiesid,
...(static values for columns)
from #table1
You can try the below query:
USE dbName
GO
INSERT INTO table2 (column_name(s))
SELECT column_name(s) FROM table1;
GO

How to combine two MS SQL Server tables into one in a stored procedure

I have a stored procedure in SQL Server 2008R2 that takes two user defined table types as parameters. Each of these types is a simple table holding an series of Ids:
CREATE TYPE [dbo].[Ids] AS TABLE(
[Id] [int] NULL
)
I want to combine the two parameters passed in to achieve the following result:
DECLARE
#Table1 TABLE (Id INT )
DECLARE
#Table2 TABLE (Id INT )
INSERT INTO #Table1 VALUES (1)
INSERT INTO #Table1 VALUES (2)
INSERT INTO #Table2 VALUES (11)
INSERT INTO #Table2 VALUES (22)
SELECT * FROM #Table1
SELECT * FROM #Table2
DECLARE
#Combined TABLE (T1 INT, T2 INT)
-- TODO: Magically combine the two tables
SELECT * FROM #Combined
-- Output would be the following
1, 11
1, 22
2, 11
2, 22
You seem to want a cross join:
insert into #Combined(t1, t2)
select t1.id, t2.id
from #Table1 t1 cross join
#Table2 t2;

How to return table from T-SQL Stored Procedure

SQL Newbie here, and I'm having a hell of a time finding what should be a simple code example to answer what I think is a simple question.
I need to write a stored procedure that does three things in order:
1) Select rows from one table
2) Update rows in another table, using values from the results table in #1
3) Return the results table from #1.
What I can't find is any example about how to return a value like this from a stored procedure. Also, how to retrieve that returned table from the caller (which is another T-SQL script).
Have a look at this.
DECLARE #Table1 TABLE(
ID INT,
VAL int
)
INSERT INTO #Table1 (ID,VAL) SELECT 1, 1
INSERT INTO #Table1 (ID,VAL) SELECT 2, 2
INSERT INTO #Table1 (ID,VAL) SELECT 3, 3
DECLARE #Table2 TABLE(
ID INT,
VAL VARCHAR(MAX)
)
INSERT INTO #Table2 (ID,VAL) SELECT 1, 1
INSERT INTO #Table2 (ID,VAL) SELECT 2, 2
INSERT INTO #Table2 (ID,VAL) SELECT 3, 3
--Lets say this is the 2 tables
--now this will go into the sp
UPDATE #Table1
SET Val = t1.Val + t2.Val
FROM #Table1 t1 INNER JOIN
#Table2 t2 ON t1.ID = t2.ID
SELECT t1.*
FROM #Table1 t1 INNER JOIN
#Table2 t2 ON t1.ID = t2.ID
--and you can insert into a var table in the tsql script that calls the sp
DECLARE #Table1TSQL TABLE(
ID INT,
VAL int
)
INSERT INTO #Table1TSQL (ID,VAL) EXEC YourSP

how to insert data into table from stored proc, manual values together in a single insert statement

Can anyone tell me how to insert a record in a table in the following case:
I have 2 tables:
create table #temp1(c4 int, c5 int,c3 int)
...and:
create table #temp2(c1 int, c2 int)
create procedure sptemp
as
begin
select c1,c2 from #temp2
end
Now I want to insert records into #temp1 table using the procedure as:
insert into #temp1(c4,c5,c3)
In the above statement, first 2 values(c4,c5) should be from procedure(exec sptemp) and the third value will be using (ex: values(34)).
Please suggest me the way to implement.
In Sql Server 2005 you can exec a sp into a table var
DECLARE #TBL TABLE(
C1 INT,
C2 INT
)
INSERT INTO #TBL (C1, C2) EXEC sptemp
SELECT *, 34 FROM #TBL