Nesting stored procedures - sql

I need create a stored proc that will return a list of a code, and then I need to call another stored proc to review each code, one by one.
How can I do this?
CREATE PROCEDURE [dbo].[paBltBuscarBoletasASA] #id_Asa int
AS
DECLARE #Query int, #Contador int
SET #Contador = 0
BEGIN
SET NOCOUNT ON;
SET #Query = (
SELECT
localizacion.c_Fk_IdBoleta
FROM
Blt_Boleta as boleta, Fnc_Localizacion as localizacion
WHERE
boleta.c_Pk_IdBoleta = localizacion.c_Fk_IdBoleta AND
localizacion.si_CodAsa = #id_Asa) //This query give the list of Codes. For example 45550711, 40480711, 80110711... etc
exec dbo.paBltMarcarErroresBoleta #Query //And here I need send one by one that list of Codes
END

You may consider adding an scalar function and call it in your query, like:
SELECT
localizacion.c_Fk_IdBoleta,
dbo.checkCode(localizacion.c_Fk_IdBoleta) as Check
FROM
Blt_Boleta as boleta, Fnc_Localizacion as localizacion
WHERE
boleta.c_Pk_IdBoleta = localizacion.c_Fk_IdBoleta AND
localizacion.si_CodAsa = #id_Asa

Declare a CURSOR for the query that you are setting equal to #Query, and then insert into the variable each subsequent value in a WHILE ##FETCH_STATUS = 0 loop. Then pass the #Query variable to the second stored procedure as you are currently doing. Here is an example:
DECLARE myCursor CURSOR FOR
SELECT localizacion.c_Fk_IdBoleta
FROM Blt_Boleta as boleta, Fnc_Localizacion as localizacion
WHERE boleta.c_Pk_IdBoleta = localizacion.c_Fk_IdBoleta AND
localizacion.si_CodAsa = #id_Asa
OPEN myCursor
FETCH NEXT FROM myCursor INTO #Query
WHILE ##FETCH_STATUS = 0
BEGIN
exec dbo.paBltMarcarErroresBoleta #Query
//do additional processing
FETCH NEXT FROM myCursor INTO #Query
END
CLOSE myCursor
DEALLOCATE myCursor
Additional cursor help: http://msdn.microsoft.com/en-us/library/ms180169.aspx

Related

Can I create a table with columns named similar to field value of another table, return by SELECT statement

Question Image
When I execute this script, there is a syntax error. What is wrong with this?
use TEST
go
CREATE TABLE newTagsTable
(
(SELECT TEST.dbo.dynamicTags.Alais
FROM TEST.dbo.dynamicTags
WHERE ID = 2) varchar(200)
);
Is this what you need ?
SELECT Alais
into newTagsTable
FROM dynamicTags
WHERE ID = 2;
DEMO
Ok, so something like this then:
create procedure test_proc
as
declare #p_sql varchar(2000);
declare #p_sql_2 varchar(2000);
declare #getid CURSOR;
SET #getid = CURSOR FOR
SELECT Alais
FROM dynamicTags;
begin
set #p_sql = 'create table newTagsTable (';
OPEN #getid
FETCH NEXT
FROM #getid INTO #p_sql_2
WHILE ##FETCH_STATUS = 0
begin
set #p_sql = #p_sql + #p_sql_2 + ' varchar(20),'
FETCH NEXT FROM #getid INTO #p_sql_2
end;
set #p_sql = left(#p_sql, len(#p_sql)-1) + ')';
EXEC (#p_sql);
end;
And then just execute the procedure:
exec test_proc
You can add parameters to this to make it more usable for orher situations...
Here is the demo for this second option:
DEMO

How do I loop through the results of a SELECT statement sql and use the result for stored procedure?

I want to use the result of a select query into a stored procedure by using cursor but it doesn't work.
Here is my code:
DECLARE #NumberPhone varchar(50)
DECLARE CUR CURSOR STATIC FOR
SELECT MobilePhone
FROM info_client
OPEN CUR
FETCH NEXT FROM CUR INTO #NumberPhone
WHILE ##FETCH_STATUS = 0
BEGIN
DECLARE #return_value int, #idCli int, #sCode varchar(2)
EXEC #return_value = StoredP_Test
#sLignePhone = #NumberPhone,
#sIMEI ='00000000000000000',
#idCli = #idCli OUTPUT,
#sCode = #sCode OUTPUT
SELECT #idCli as N'#idCli',
#sCode as N'#sCode'
FETCH NEXT FROM CUR INTO #NumberPhone
SELECT 'Return Value' = #return_value
END
CLOSE CUR
DEALLOCATE CUR
This code duplicate the same result as the number of line of the select query.
Your code has an initial fetch of
FETCH NEXT FROM CUR INTO #NumberPhone
Your code has a fetch next inside the loop
FETCH NEXT FROM CUR INTO #sLigneAssValue
Didn't you want to fetch next into the #NumberPhone variable?
The #NumberPhone variable will never change.

SQL Server 2014 - sp_describe_cursor_column for dynamic queries issue

Basically I want to know columns/aliases of result set from dynamic query. I tried to use sp_describe_cursor_column but without success.
It returns me that cursor does not exits. But I can fetch values from such cursor...
The code is :
ALTER PROC TestProc
AS
DECLARE #dynamicSQL nvarchar(200)
-- Have code that will construct the dynamic SQL
SET #dynamicSQL = ' select table_name, TABLE_TYPE from INFORMATION_SCHEMA.TABLES'
-- The cursor that will be filled by the dynamic SQL
DECLARE #outputCursor CURSOR
-- Create the dynamic SQL to fill a CURSOR instead
SET #dynamicSQL = 'SET #outputCursor = CURSOR FORWARD_ONLY STATIC FOR ' +
#dynamicSQL + ' ; OPEN #outputCursor'
-- Execute dynamic sql
exec sp_executesql -- sp_executesql will essentially create a sproc
#dynamicSQL, -- The SQL statement to execute (body of sproc)
N'#outputCursor CURSOR OUTPUT', -- The parameter list for the sproc: OUTPUT CURSOR
#outputCursor OUTPUT -- The parameter to pass to the sproc: the CURSOR
declare #Report cursor
exEC sp_describe_cursor_columns
#cursor_return = #Report OUTPUT
,#cursor_source = N'local'
,#cursor_identity = N'outputCursor';
-- Code that will just output the values from the cursor
DECLARE #tableName nvarchar(200), #table_type nvarchar(200);
FETCH NEXT FROM #outputCursor INTO #tableName, #table_type
-- Loop while there're more things in the cursor
WHILE ##FETCH_STATUS = 0
BEGIN
PRINT #tableName
FETCH NEXT FROM #outputCursor INTO #tableName, #table_type
END
-- Be nice, close & deallocate cursor
CLOSE #outputCursor
DEALLOCATE #outputCursor
And this is the result:
Msg 16916, Level 16, State 4, Procedure sp_describe_cursor_columns,
Line 23 A cursor with the name 'outputCursor' does not exist.
DATABASE_UPDATE
SYSTEM_CONFIGURATION ....
I want as result to see table_name , table_type.
Don't tell me that I can just extarct it from string, becasue user may send select * from xxxx.
I found some other way how to extract description of result set for dynamic queries.
declare #Table nvarchar(200) ;
DECLARE #dynamicSQL nvarchar(200)
SET #dynamicSQL = 'select table_name, TABLE_TYPE from INFORMATION_SCHEMA.TABLES'
SELECT #Table = COALESCE(#Table + ', ', '') + Name
FROM sys.dm_exec_describe_first_result_set
(#dynamicSQL, NULL,1)
print #Table

How to EXEC a stored procedure using values from a table

I have a proc that retrieves data from a table based on some conditions and inserts that into a temp table.
I now want to execute another proc using the values from my temp table, which will have about 2000 entries.
Can I do this in a single EXEC and SELECT all the entries from my #Temp table?
I want to do something like this:
DECLARE #return_value int
EXEC #return_value = [dbo].[prInsertAssessmentLevels]
#WFRouteItemID = (SELECT WFRouteItemID FROM #WFsNotInWFAssessmentLevel),
#UOB = (SELECT UOB FROM #WFsNotInWFAssessmentLevel WHERE WFRouteItemID = #WFRouteItemID),
#Life = (SELECT Life FROM #WFsNotInWFAssessmentLevel WHERE WFRouteItemID = #WFRouteItemID),
#Health = (SELECT Health FROM #WFsNotInWFAssessmentLevel WHERE WFRouteItemID = #WFRouteItemID),
#Disability = (SELECT Disability FROM #WFsNotInWFAssessmentLevel WHERE WFRouteItemID = #WFRouteItemID),
#CreatedUserID = 1
You'll need a cursor
DECLARE #return_value int
DECLARE #WFRouteItemID int /* Or whatever the correct Type is*/
DECLARE #UOB int /* Or whatever the correct Type is*/
DECLARE #Life int /* Or whatever the correct Type is*/
DECLARE #Health int /* Or whatever the correct Type is*/
DECLARE #Disability int /* Or whatever the correct Type is*/
DECLARE #CreatedUserID int /* Or whatever the correct Type is*/
DECLARE PARAM_CURSOR CURSOR FOR
SELECT WFRouteItemID, UOB, Life, Health, Disability, CreatedUserID
FROM #WFsNotInWFAssessmentLevel
OPEN PARAM_CURSOR
FETCH NEXT FROM PARAM_CURSOR INTO #WFRouteItemID, #UOB, #Life, #Health, #Disability, #CreatedUserID
WHILE ##FETCHSTATUS = 0
BEGIN
EXEC #return_value = [dbo].[prInsertAssessmentLevels],#WFRouteItemID, #UOB, #Life, #Health, #Disability, #CreatedUserID
FETCH NEXT FROM PARAM_CURSOR INTO #WFRouteItemID, #UOB, #Life, #Health, #Disability, #CreatedUserID
END
CLOSE PARAM_CURSOR
DEALLOCATE PARAM_CURSOR
what you want to do is more like going through your table row-by-row and executing the stored procedure with all the row values. let me give you a template for a cursor-based approach:
DECLARE curName CURSOR FOR
SELECT col1, col2, col3
FROM your_table
OPEN curName
FETCH NEXT FROM curName INTO #pk
WHILE ##FETCH_STATUS = 0
BEGIN
EXEC your_Procedure (#par1 = #col1, #par2 = #col2, #par3 = #col3)
FETCH NEXT FROM curName INTO #col1, #col2, #col3
END
CLOSE curName
DEALLOCATE curName

execute procedure for a sequence of elements (execute theProc (select id from table))

I need to execute the procedure deleteQuestion for each element that was returned by this select query:
select id from questions where Stuff = #Stuff
execute deleteQuestion id
something like:
execute deleteQuestion each(select id fom questions where Stuff = #Stuff)
anybody knows how ?
Use a cursor:
DECLARE #Id INT
DECLARE your_cursor CURSOR FOR
SELECT id from questions where Stuff = #Stuff
OPEN your_cursor
FETCH NEXT FROM your_cursor
INTO #Id
WHILE ##FETCH_STATUS = 0
BEGIN
execute deleteQuestion #Id
FETCH NEXT FROM your_cursor
INTO #Id
END
CLOSE your_cursor
DEALLOCATE your_cursor
I did it like this:
declare #sqlstr nvarchar(max)
set #sqlstr = ''
select #sqlstr = #sqlstr + ' exec deleteQuestion ' + cast(q.id as nvarchar(max))
from Questions q where stuff = #stuff
exec (#sqlstr)
I've been told that this approach is much faster than a cursor