How to pass an int column to a procedure which take an int parameter - sql

I have a stored procedure MyProcedure which needs an int as parameter, now I need to pass an int column from another table as parameter to the procedure. How can I write a statement to implement that? I can't use cursor in my code for some reasons. Below is my pseudo code. Thanks.
Create Procedure MyProcedure
#i_input INT
AS
... ...
create table MyTable
(
id int,
otherdata float
)
exec MyProcedure (select id from MyTable)

Go for function because you can't run Procedure through DML/DDL
CREATE TABLE MyResult
(
id INT,
results VARCHAR(MAX)
)
Go
CREATE FUNCTION MyFunction(#i_input INT)
RETURNS TABLE
AS RETURN
(
SELECT #i_input AS ID, 'result1' AS results
)
Go
INSERT INTO MyResult SELECT * FROM MyFunction(1)

You can do like this
Create Procedure MyProcedure
#i_input INT
AS
... ...
create table MyTable
(
id int,
otherdata float
)
Declare #temp_id int
select #temp_id =id from MyTable
exec MyProcedure #temp_id

Related

Doing a for-each record in a user defined table type in Stored Procedure

:)
I have this defined type:
CREATE TYPE dbo.MyType
AS TABLE
(
name varchar(255),
value varchar(255)
);
Having this stored procedure:
CREATE PROCEDURE MyProcedure #docName varchar(255), #docPath varchar(255), #values AS [dbo].MyType Readonly
AS
declare #ID table (ID int)
INSERT INTO MyTable output inserted.fileID into #ID values (#docName,#docPath)
-- insert loop here
GO;
And the following "one to many" table
CREATE TABLE TableN (
fileID int PRIMARY KEY,
name varchar(255),
value varchar(255)
)
How can I, where it is noted in the above code, make a loop in order to for each record in the MyType table, to insert it into TableN, together with the fileID from the insert?
Thanks!
There's no need for a loop (you need to stop thinking programmatically in SQL and think in datasets). Just do an INSERT:
INSERT INTO TableN (FileID,[name],[value])
SELECT ID.ID,
V.[Name],
V.[value]
FROM #values v
CROSS JOIN #ID ID;

Insert stored procedure plus static value

I am trying to insert the results from multiple stored procedures into a temp table. However I would also like to add another static value in each instance that is not part of these results to the table, but I am not sure about the syntax to do this.
Here's where I am at so far:
CREATE TABLE #table (
STATIC INT, --STATIC NUMBER
SP1 INT, --STORED PROCEDURE COL 1
SP2 INT, --STORED PROCEDURE COL 2
SP3 INT, --STORED PROCEDURE COL 3
);
INSERT INTO #table values('1'), EXEC stored_procedure 'param_1', 'param_2';
INSERT INTO #table values('2'), EXEC stored_procedure 'param_3', 'param_4';
Any suggestions would be great!
You can't quite do it the way you want, but you could INSERT,UPDATE, INSERT,UPDATE.
CREATE TABLE #table (
STATIC INT, --STATIC NUMBER
SP1 INT, --STORED PROCEDURE COL 1
SP2 INT, --STORED PROCEDURE COL 2
SP3 INT, --STORED PROCEDURE COL 3
);
INSERT INTO #table (SP1,SP2,SP3)
EXEC stored_procedure 'param_1', 'param_2';
UPDATE #table
SET STATIC = 1
WHERE STATIC IS NULL;
INSERT INTO #table (SP1,SP2,SP3)
EXEC stored_procedure 'param_3', 'param_4';
UPDATE #table
SET STATIC = 2
WHERE STATIC IS NULL;
Assuming that the Stored procedure's are returning 3 columns in that order.
Insert into a temp table and then join:
CREATE TABLE #sp (
SpKey INT IDENTITY(1,1) NOT NULL, --use for join
SP1 INT, --STORED PROCEDURE COL 1
SP2 INT, --STORED PROCEDURE COL 2
SP3 INT, --STORED PROCEDURE COL 3
);
CREATE TABLE #static (
StaticKey INT IDENTITY(1,1) NOT NULL, --use for join
Value INT
);
INSERT INTO #sp EXEC stored_procedure 'param_1', 'param_2'
INSERT INTO #static VALUES (1)
INSERT INTO #sp EXEC stored_procedure 'param_3', 'param_4'
INSERT INTO #static VALUES (2)
SELECT * INTO #table
FROM
(
SELECT *
FROM #static
INNER JOIN #sp ON #static.StaticKey = #sp.SpKey) sub

SQL Server insert based on the execution results of a stored procedure [duplicate]

This question already has answers here:
Insert results of a stored procedure into a temporary table
(33 answers)
Closed 7 years ago.
I have a table variable with two columns. The first column is a value that I am trying to populate while the second value is being populated by the execution of a stored procedure.
CREATE PROCEDURE [dbo].userProfiles
(#userID INT) AS
BEGIN
DECLARE #sampleTable TABLE (moduleName VARCHAR(20),
userProfile int)
INSERT INTO #sampleTable('userprofile', exec getUserProfile(#userID))
SELECT *
FROM #sampleTable
END
However, I keep getting this error whenever I try executing the stored procedure:
Level 15, State 1, Procedure userProfiles, Line 9
[Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'userprofile'.
Any help will be well appreciated .
Thanks in advance
Probably your SP do have a select statement,
see: SQL Server - SELECT FROM stored procedure
CREATE PROCEDURE [dbo].userProfiles(
#userID INT
) AS
BEGIN
DECLARE #sampleTable TABLE (
moduleName VARCHAR(20),
userProfile int
)
DECLARE #stored_proc_table TABLE (
clmn datatype --similar as result set from getUserProfile
)
insert into #stored_proc_table exec getUserProfile(#userID)
INSERT INTO #sampleTable
select 'userprofile', clmn from #stored_proc_table;
SELECT * FROM #sampleTable
END
My guess is you need end your statement with semicolon ;
DECLARE #local_variable (Transact-SQL)
DECLARE #MyTableVar table(
EmpID int NOT NULL,
OldVacationHours int,
NewVacationHours int,
ModifiedDate datetime);
Missing values. Also, consider using out parameter to get value from procedure.
CREATE PROCEDURE [dbo].userProfiles(
#userID INT) AS
BEGIN
DECLARE #sampleTable TABLE(
moduleName VARCHAR(20),
userProfile int)
Create table #valueholder(resultvalue int)
insert into #valueholder exec getUserProfile(#userID)
INSERT
INTO #sampleTable
select 'userprofile',resultvalue from #valueholder
SELECT *
FROM #sampleTable
END
Also, you cannot make an inline call to procedure. use out parameter.
here is an example about out param.
https://technet.microsoft.com/en-us/library/ms187004(v=sql.105).aspx

How can I insert the results of a stored procedure into a new table

ALTER procedure [dbo].[staffscorecard]
#STAFF_ID INT = NULL
as
select
count(STAFF_ID) as countexel
from
TbStudentSurvey
where
FEEDBACK = 'excellent'
and STAFF_ID = ISNULL(#STAFF_ID, STAFF_ID)
select
Score as scoreexel
from
TbStaffScoreMaster
where
Status = 'Excellent'
exec [dbo].[staffscorecard]
GO
CREATE TABLE #temp ( countexel int, scoreexel int)
GO
INSERT INTO #temp (countexel , scoreexel)
EXEC [dbo].[staffscorecard]
GO
SELECT *
FROM #temp
GO
For a given staffid to calculate countexel and scoreexel you can re-write your stored procedure as:
create table TbStudentSurvey (STAFF_ID int,FEEDBACK varchar(20));
insert into TbStudentSurvey values (1,'excellent'),(1,'excellent'),(2,'excellent');
create table TbStaffScoreMaster (Score int,[Status] varchar(20));
insert into TbStaffScoreMaster values(100,'Excellent');
Go
create procedure [dbo].[staffscorecard]
#STAFF_ID INT = NULL,
#countexel int output,-- Explicitly declare output variables to fetch these values
#scoreexel int output
as
Begin
select
#countexel = count(STAFF_ID)
from
TbStudentSurvey
where
FEEDBACK = 'excellent'
and STAFF_ID = ISNULL(#STAFF_ID, STAFF_ID)
select
#scoreexel = Score
from
TbStaffScoreMaster
where
Status = 'Excellent'
End
GO
and then instead of using a temp table use a table variable because when you use temp tables,the table has to match the exact column layout as of the stored procedure.
--CREATE TABLE #temp (countexel int, scoreexel int)
--GO
--Create a table variable:
declare #temp table (countexel int, scoreexel int)
declare #countexel int, #scoreexel int,#STAFF_ID int;
--set value of staff Id for which you want to get countexel and scoreexel.
set #STAFF_ID = 1;
EXEC [dbo].[staffscorecard] #STAFF_ID ,#countexel output,#scoreexel output
INSERT #temp values (#countexel ,#scoreexel);
SELECT *
FROM #temp
GO
Method 2:
You can also write as:
alter procedure [dbo].[staffscorecard]
#STAFF_ID INT = NULL
as
Begin
select
count(STAFF_ID) as countexel , Score as scoreexel
from
TbStudentSurvey TSS
inner join TbStaffScoreMaster TSM on TSM.Status = TSS.FEEDBACK
where
FEEDBACK = 'excellent'
and STAFF_ID = ISNULL(#STAFF_ID, STAFF_ID)
group by STAFF_ID,Score
End
GO
declare #temp table (countexel int, scoreexel int)
declare #STAFF_ID int;
set #STAFF_ID = 1;--set value of staff Id for which you want to get countexel and scoreexel.
INSERT #temp EXEC [dbo].[staffscorecard] #STAFF_ID
SELECT *
FROM #temp
GO
Hope this helps!!

Inserting into table the values returned from stored procedure

I have a procedure which has a select statement as below:
CREATE PROCEDURE pr_test
as
SELECT * from SOURCETABLE
Can I insert into a temp table by executing the stored procedure pr_test by any chance?
You can use INSERT EXEC
declare #t results (field1 int, ....)
insert #t (field1,...)
exec pr_test
CREATE PROCEDURE pr_test
as
begin
CREATE TABLE #TibetanYaks(
YakID int,
YakName char(30) )
INSERT INTO #TibetanYaks (YakID, YakName)
SELECT YakID, YakName
FROM dbo.Yaks
WHERE YakType = 'Tibetan'
-- Do some stuff with the table
drop table #TibetanYaks
end