Passing multiple values to parameters from an existing table - sql-server

I am trying to create a stored procedure which passes values from select statement of joined tables as parameters to another stored procedure inside to create return credit for returned purchase which are assigned in the WHERE clause of joined tables (#RANO and #returndate). I want to pass many customers (as #Customer) from the joined tables to the stored procedure CreateManCreditHDFromReturn but it picks the first #RANO in the WHERE clause of the joined statement!
How can I pass multiple values (like customers, divisions,...) from all RANOs in the where clause of the joined table?
Note: RANO mean return authorization of returned purchase.
ALTER PROCEDURE [dbo].[CreateEllieAutoCreditHDFromReturn]
AS
BEGIN
DECLARE #ARReason nVarChar(5),
#DocumentDate Datetime,
#ARDocumentType VarChar(5),
#TRANSACTIONID INT,
#TIMELASTMOD DATETIME,
#USERIDLASTMOD nvarchar(5),
#CompanyCode nVarChar(10),
#Customer nVarChar(10),
#Division nVarChar(5),
#Warehouse nVarChar(10),
#Salesman1 nVarChar(10),
#Currency nVarChar(3),
#ReturnNo int,
#RANO int,
#Style nvarchar(15),
SELECT #Pkey = #CompanyCode = rh.companycode,
#Customer = rh.CUSTOMER,
#Division = rh.DIVISION,
#Warehouse = rh.WAREHOUSE,
#Salesman1 = rh.SALESMAN1,
#Currency = rh.CURRENCY,
#ReturnNo = rh.returnno,
#RANO = rh.RANO
FROM
ReturnDetail rd
INNER JOIN
ReturnHeader rh ON rh.RETURNNO = rd.RETURNNO
WHERE
rh.RETURNDATE >= '2020-03-27 00:00:00'
AND rh.RETURNDATE < '2020-03-28 00:00:00'
AND rd.RANO IN (79383, 79820)
AND rd.ARDOCUMENTNOCOMPANY = 0
SET #ARReason = 'RM'
SET #ARDocumentType = 'RCRDT'
SET #DocumentDate = GETDATE()
SET #TRANSACTIONID = 0
SET #TIMELASTMOD = GETDATE()
SET #USERIDLASTMOD = 'WSchan'
SET #ReferenceNo = 0
DECLARE #CurrDocumentNo Int,#CurrDocumentNoCompany Int
EXEC dbo.CreateManCreditHDFromReturn
#CompanyCode,
#ARReason,
#Customer,
#Division,
#Warehouse,
#Salesman1,
#Currency,
#DocumentDate,
#ReferenceNo,
#ARDocumentType,
#CurrDocumentNo OUTPUT,
#CurrDocumentNoCompany OUTPUT,
#TRANSACTIONID,
#TIMELASTMOD,
#USERIDLASTMOD
END

You can't assign multiple values to scalar variables. If you are restricted in that you have no access to modify dbo.CreateManCreditHDFromReturn then you have to use a cursor or loop and iterate through each record in your query. The stored procedure is coded to accept one value at a time for its input parameters.
If you have the ability to modify dbo.CreateManCreditHDFromReturn then you can update it to accept a table variable, JSON, or XML and then inside that stored procedure you can unpack the input and process it accordingly.
If you have to use a cursor, which I'm guessing is going to be the case, it would look something like this:
DECLARE #CompanyCode NVARCHAR(10);
[...and other variables...]
DECLARE RANOCURS CURSOR FOR
SELECT CompanyCode, [...and other fields...]
FROM ReturnDetail rd
INNER JOIN ReturnHeader rh ON rh.RETURNNO = rd.RETURNNO
WHERE rh.RETURNDATE >= '2020-03-27 00:00:00'
AND rh.RETURNDATE< '2020-03-28 00:00:00'
AND rd.RANO IN (79383, 79820)
AND rd.ARDOCUMENTNOCOMPANY = 0;
OPEN RANOCURS;
FETCH NEXT FROM RANOCURS INTO #CompanyCode, [...other variables...]
DECLARE #CurrDocumentNo INT, #CurrDocumentNoCompany INT;
WHILE ##FETCH_STATUS = 0
BEGIN
EXEC dbo.CreateManCreditHDFromReturn
#CompanyCode ,
[...other variable parameters...],
#CurrDocumentNo OUTPUT,
#CurrDocumentNoCompany OUTPUT;
[...do something with the return values...]
FETCH NEXT FROM RANOCURS INTO #CompanyCode, ...[other fields]
END;
CLOSE RANOCURS;
DEALLOCATE RANOCURS;

Related

MSSQL2016 UPDATE trigger to INSERT record in other table based on SUM of bit column

I'm attempting to automate our order status update system. Here is the flow we have in place:
orders come in to our system as one order (via FTP file), and get split into 2 or more orders (inserted into SplitOrdersHeader_tb)
resulting multiple orders are sent from the SplitOrders tables (header and detail) to ERP system
ERP system produces order confirmations for each order (inserted into OrderConfirmationHeader_tb, and detail table)
need to update original single order status after all split order confirmations have been received
Here are the tables involved:
CREATE TABLE SplitOrdersHeader_tb(
OriginalCustomerPONumber varchar(20),
NewCustomerPONumber varchar(20),
Company varchar(2),
CustomerNumber varchar(10),
OrderProcessed bit DEFAULT 0,
OrderMoved bit DEFAULT 0,
OrderConfirmationReceived bit DEFAULT 0
)
CREATE TABLE OrderConfirmationHeader_tb(
MasterOrderNumber varchar(20),
CustomerPONumber varchar(20),
Company varchar(2),
CustomerNumber varchar(10)
)
CREATE TABLE UpdateOtherSystem_tb(
OriginalCustomerPONumber varchar(20)
)
I have a trigger on the OrderConfirmationHeader_tb that updates the status of each split order, once the order confirmations have been loaded:
CREATE TRIGGER dbo.INSERT_Update_SplitOrderConfirmations_tg
ON dbo.OrderConfirmationHeader_tb
AFTER INSERT
AS
SET NOCOUNT ON
BEGIN
UPDATE SOH
SET SOH.OrderConfirmationReceived = 1,
SOH.MasterOrderNumber = LTRIM(RTRIM(I.MasterOrderNumber))
FROM OrderSplitting.SplitOrdersHeader_tb SOH
INNER JOIN inserted I ON SOH.CustomerNumber = I.Customer
AND SOH.NewCustomerPONumber = I.Reference
AND SOH.Company = I.Company
AND SOH.OrderProcessed = 1
AND SOH.OrderMoved = 1
END
What I'm wanting to do is create an UPDATE trigger on the SplitOrdersHeader_tb that will:
- count the number of split orders from the original CustomerPONumber
- sum the number of OrderConfirmationReceived values
- if COUNT = SUM then insert a new record into UpdateOtherSystem_tb, provided the MasterOrderNumber does not already exist in the UpdateOtherSystem_tb
I have this, but it feels way too clunky:
CREATE TRIGGER OrderSplitting.UPDATE_Update_WCO_Status_tg
ON OrderSplitting.SplitOrdersHeader_tb
AFTER UPDATE AS
SET NOCOUNT ON
BEGIN
DECLARE #NEW_CUSTOMER_PO_NUMBER varchar(255),
#ORIGINAL_CUSTOMER_PO_NUMBER varchar(255),
#COUNT_OF_ORDER_HEADERS int,
#TOTAL_CONFIRMED_ORDERS int
SELECT #NEW_CUSTOMER_PO_NUMBER = NewCustomerPONumber
FROM inserted
SELECT #ORIGINAL_CUSTOMER_PO_NUMBER = OriginalCustomerPONumber,
#COUNT_OF_ORDER_HEADERS = COUNT(*),
#TOTAL_CONFIRMED_ORDERS = SUM(CAST(OrderConfirmationReceived as int))
FROM OrderSplitting.SplitOrdersHeader_tb
WHERE OriginalCustomerPONumber IN (SELECT OriginalCustomerPONumber
FROM OrderSplitting.SplitOrdersHeader_tb
WHERE NewCustomerPONumber = #NEW_CUSTOMER_PO_NUMBER)
GROUP BY OriginalCustomerPONumber
IF #COUNT_OF_ORDER_HEADERS = #TOTAL_CONFIRMED_ORDERS
BEGIN
BEGIN TRY
INSERT INTO OrderSplitting.UpdateOtherSystem_tb(OriginalCustomerPONumber)
VALUES(#ORIGINAL_CUSTOMER_PO_NUMBER)
END TRY
BEGIN CATCH
DECLARE #ERROR_MESSAGE varchar(MAX)
SET #ERROR_MESSAGE = ERROR_MESSAGE()
EXEC msdb..sp_send_dbmail
#recipients = <app_support>,
#subject = 'Update Trigger Error',
#body = #ERROR_MESSAGE
END CATCH
END
END
I think I have something, but would like some additional feedback, please:
CREATE TRIGGER OrderSplitting.UPDATE_Update_WCO_Status_tg
ON OrderSplitting.SplitOrdersHeader_tb
AFTER UPDATE AS
SET NOCOUNT ON
BEGIN TRY
INSERT INTO OrderSplitting.UpdateOtherSystem_tb(OriginalCustomerPONumber)
SELECT SOH.OriginalCustomerPONumber
FROM OrderSplitting.SplitOrdersHeader_tb SOH
INNER JOIN inserted I ON SOH.OriginalCustomerPONumber = I.OriginalCustomerPONumber
LEFT OUTER JOIN OrderSplitting.UpdateOtherSystem_tb UOS ON SOH.OriginalCustomerPONumber = UOS.OriginalCustomerPONumber
WHERE UOS.OriginalCustomerPONumber IS NULL
GROUP BY SOH.OriginalCustomerPONumber
HAVING COUNT(SOH.OriginalCustomerPONumber) = SUM(CAST(SOH.OrderConfirmationReceived as int))
END TRY
BEGIN CATCH
DECLARE #ERROR_MESSAGE varchar(MAX)
SET #ERROR_MESSAGE = ERROR_MESSAGE()
EXEC msdb..sp_send_dbmail
#recipients = <app_support>,
#subject = 'Update Trigger Error',
#body = #ERROR_MESSAGE
END CATCH

SQL Server - update multiple records using a stored procedure

Being a super novice at this, I would like some guidance on this, please.
I need to compare two sets of data and update one set with a value. This is what I have so far.
PROCEDURE [dbo].[update_personnel_rank]
AS
DECLARE #frsid VARCHAR
DECLARE #officerid VARCHAR
DECLARE #hrrank VARCHAR
DECLARE #personnelrank VARCHAR
DECLARE #farank VARCHAR
DECLARE #rank VARCHAR(150)
SET #rank = 'Admin Spec II'
BEGIN
SET NOCOUNT ON;
SELECT
#frsid = hr.FRSID,
#officerid = p.OfficerID,
#hrrank = hr.Rank,
#personnelrank = p.Rank,
#farank = r.FA_Rank
FROM
[FireApp_REPL_DW_Data].[dbo].[MCFRSCombinedPersonnelandPimsStaff] hr
INNER JOIN
[fh_reports].[dbo].[personnel_bk] p ON p.OfficerID = hr.FRSID
INNER JOIN
[fh_reports].[dbo].[Rank_Lookup_tbl] r ON r.FA_Rank = hr.Rank
WHERE
(p.rank <> hr.Rank OR p.rank = '')
AND p.Rank = #rank
UPDATE [fh_reports].[dbo].[personnel_bk]
SET Rank = #farank
WHERE OfficerID = #officerid
END
GO
The select query returns 3 records and this stored procedure runs without any error, but it does not update the records. Since the select query returns 3 records, I think I need to change the parameter setting accordingly, but not sure how...
To #Sami's point, if you are not returning those variables, you do not need to set them and can instead just run the update:
USE [YourDatabase]
GO
SET NOCOUNT ON
GO
ALTER PROCEDURE [dbo].[update_personnel_rank]
#rank VARCHAR(150) --= 'Admin Spec II'
AS
BEGIN
IF #rank IS NULL OR #rank = ''
RAISERROR('Please enter a valid rank string.', 16, 1)
UPDATE hr
SET [Rank] = r.FA_Rank
FROM [FireApp_REPL_DW_Data].[dbo].[MCFRSCombinedPersonnelandPimsStaff] [hr]
INNER JOIN [fh_reports].[dbo].[personnel_bk] [p]
ON [p].[OfficerID] = [hr].[FRSID]
INNER JOIN [fh_reports].[dbo].[Rank_Lookup_tbl] [r]
ON [r].[FA_Rank] = [hr].[Rank]
WHERE [p].[rank] <> [hr].[Rank]
AND ([p].[Rank] = #rank OR p.[Rank] = '')
END ;
GO

How to get and use the value returned by a stored procedure to a INSERT INTO... SELECT... statement

I am just new in SQL language and still studying it. I'm having hard time looking for answer on how can I use the stored procedure and insert value into a table.
I have this stored procedure:
CREATE PROCEDURE TestID
AS
SET NOCOUNT ON;
BEGIN
DECLARE #NewID VARCHAR(30),
#GenID INT,
#BrgyCode VARCHAR(5) = '23548'
SET #GenID = (SELECT TOP (1) NextID
FROM dbo.RandomIDs
WHERE IsUsed = 0
ORDER BY RowNumber)
SET #NewID = #BrgyCode + '-' + CAST(#GenID AS VARCHAR (30))
UPDATE dbo.RandomIDs
SET dbo.RandomIDs.IsUsed = 1
WHERE dbo.RandomIDs.NextID = #GenID
SELECT #NewID
END;
and what I'm trying to do is this:
INSERT INTO dbo.Residents([ResidentID], NewResidentID, [ResLogdate],
...
SELECT
[ResidentID],
EXEC TestID ,
[ResLogdate],
....
FROM
source.dbo.Resident;
There is a table dbo.RandomIDs containing random 6 digit non repeating numbers where I'm pulling out the value via the stored procedure and updating the IsUsed column of the table to 1. I'm transferring data from one database to another database and doing some processing on the data while transferring. Part of the processing is generating a new ID with the required format.
But I can't get it to work Sad I've been searching the net for hours now but I'm not getting the information that I need and that the reason for my writing. I hope someone could help me with this.
Thanks,
Darren
your question is little bit confusing, because you have not explained what you want to do. As i got your question, you want to fetch random id from randomids table and after performed some processing on nextid you want to insert it into resident table [newresidentid] and end of the procedure you fetch data from resident table. if i get anything wrong feel free to ask me.
your procedure solution is following.
CREATE PROCEDURE [TestId]
AS
SET NOCOUNT ON;
BEGIN
DECLARE #NEWID NVARCHAR(30)
DECLARE #GENID BIGINT
DECLARE #BRGYCODE VARCHAR(5) = '23548'
DECLARE #COUNT INTEGER
DECLARE #ERR NVARCHAR(20) = 'NO IDS IN RANDOM ID'
SET #COUNT = (SELECT COUNT(NEXTID) FROM RandomIds WHERE [IsUsed] = 0)
SET #GENID = (SELECT TOP(1) [NEXTID] FROM RandomIds WHERE [IsUsed] = 0 ORDER BY [ID] ASC)
--SELECT #GENID AS ID
IF #COUNT = 0
BEGIN
SELECT #ERR AS ERROR
END
ELSE
BEGIN
SET #NEWID = #BRGYCODE + '-' + CAST(#GENID AS varchar(30))
UPDATE RandomIds SET [IsUsed] = 1 WHERE [NextId] = #GENID
INSERT INTO Residents ([NewResidentId] , [ResLogDate] ) VALUES (#NEWID , GETDATE())
SELECT * FROM Residents
END
END
this procedure will fetch data from your randomids table and perform some processing on nextid than after it directs insert it into resident table and if you want to insert some data through user you can use parameter after declaring procedure name
E.G
CREATE PROCEDURE [TESTID]
#PARAM1 DATATYPE,
#PARAM2 DATATYPE
AS
BEGIN
END
I'm not convinced that your requirement is a good one but here is a way to do it.
Bear in mind that concurrent sessions will not be able to read your update until it is committed so you have to kind of "lock" the update so you will get a block until you're going to commit or rollback. This is rubbish for concurrency, but that's a side effect of this requirement.
declare #cap table ( capturedValue int);
declare #GENID int;
update top (1) RandomIds set IsUsed=1
output inserted.NextID into #cap
where IsUsed=0;
set #GENID =(select max( capturedValue) from #cap )
A better way would be to use an IDENTITY or SEQUENCE to solve your problem. This would leave gaps but help concurrency.

Use a stored procedure to get a list of nvarchar(20) values as a parameter

I'm developing a SQL Server 2012 stored procedure.
I need to return a list of nvarchar(20) values, and I don't know how can I do it. I have tried this:
Using a Table-Valued Parameters but it must be READONLY.
Doing a select to return that list. But stored procedures only
returns INT values.
Any idea?
UPDATE:
This is what I have done:
CREATE PROCEDURE [dbo].[GetAggregationChildren]
#parentCode nvarchar(20),
#codeLevel tinyint output,
#returnValue int output
AS
declare #childsLevelCount tinyint
, #invalidChildCodesCount int;
set nocount on;
-- ======== VALIDATION ==========
if NULLIF(#parentCode, '') IS NULL
begin
set #returnValue = -19;
return NULL; -- Parameter null or empty.
end
-- Check if parent exists in AGGREGATIONS table.
if not exists (Select top 1 CODE from AGGREGATIONS where CODE = #parentCode)
begin
set #returnValue = -11;
return NULL;
end
set #childsLevelCount = (select count(c.CODE_LEVEL) from CODES c where c.CODE in (Select CODE from AGGREGATION_CHILDS where PARENT_CODE = #parentCode) group by c.CODE_LEVEL);
-- If this aggregation has children codes with different values, it is an error.
if (#childsLevelCount > 1)
begin
set #returnValue = -201;
return NULL;
end
-- =========== CODE =============
set #returnValue = 0;
set #codeLevel = (select c.CODE_LEVEL from CODES c where c.CODE in (Select CODE from AGGREGATION_CHILDS where PARENT_CODE = #parentCode) group by c.CODE_LEVEL);
select CODE from AGGREGATION_CHILDS where PARENT_CODE = #parentCode;
But, I have no idea about how to return the result of this select:
select CODE from AGGREGATION_CHILDS where PARENT_CODE = #parentCode;
This stored procedure returns this on SQL Server Management Studio:
It is also returning a 0. I thought that the stored procedure is going to return the select result only.
I need the result in a parameter because I going to use in a SQLCommand like this one:
SqlParameter childsParam = new SqlParameter();
childsParam.ParameterName = "#childs";
childsParam.SqlDbType = System.Data.SqlDbType.Structured;
childsParam.Direction = System.Data.ParameterDirection.Input;
childsParam.Value = tvp;
parameters = new List<SqlParameter>();
parameters.Add(childsParam);
SqlConnection connection =
_context.Database.Connection as SqlConnection;
connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = storedProcedureName;
cmd.CommandType = CommandType.StoredProcedure;
if (parameters != null)
cmd.Parameters.AddRange(parameters.ToArray());
cmd.Connection = connection;
cmd.ExecuteNonQuery();
connection.Close();
Stored procedure returns only Integer?
No this is not 100% true. If you are using RETURN to return the values from your stored procedure then your statement is true else it is false.
If you want to return string from your stored procedure then you can use SELECT like this:
CREATE PROCEDURE myProc
AS
BEGIN
SELECT 'test'
END
And to return multiple values you can use it like
CREATE PROCEDURE myProc
#Value1 varchar(20) OUTPUT,
#Value2 varchar(20) OUTPUT
AS
SELECT #Value1 = 'test1', #Value2 = 'test2'
and call it like
DECLARE #Value1 varchar(20), #Value2 varchar(20)
exec myProc #Value1 OUTPUT, #Value2 OUTPUT
SELECT #Value1, #Value1
Stored procedures return the type of the field in the SELECT statement. You can use CAST and CONVERT to change the types. For example:
SELECT CAST(field AS NVARCHAR(20))
With table value parameters you can set the field type on creation:
CREATE TYPE JobSpecifications AS TABLE
(JobName VARCHAR(50), AvailableDate Date );
you can use a temporary table to recuperate your list from the stored procedure, like the example below :
create proc Test
AS BEGIN
SELECT CAST('jkj' AS NVARCHAR(20)) value
END
DECLARE #tmp TABLE(value nvarchar(20))
INSERT INTO #tmp EXEC GhaziTest
SELECT * from #tmp

Run A Loop in SQL Server

I want to run a stored procedure on each ID return by a SELECT query. Is there a simple way to do something like:
FOREACH (SELECT ID FROM myTABLE WHERE myName='bob') AS id
BEGIN
EXEC #return_value = [dbo].[spMYPROC]
#PARAM1 = id
#PARAM2 = 0
END
Since I just happened to answer a very similar question yesterday, I have this code handy. As others have stated, it may not be the best approach, but still it's nice to learn how to use a while loop anyway.
Assuming a table named "Customer"
declare #Id int
select #Id = MIN(Id)
from Customer c
while(select COUNT(1)
from Customer c
where c.Id >= #Id) > 0
begin
--run your sproc right here
select #Id = MIN(Id)
from Customer c
where c.Id > #Id
end
DECLARE #ID INT, #return_value INT
DECLARE c CURSOR FOR
SELECT
ID
FROM myTABLE
WHERE myName = 'bob'
OPEN c; FETCH NEXT FROM c INTO #ID
WHILE ##FETCH_STATUS = 0
BEGIN
EXEC #return_value = [dbo].[spMYPROC]
#PARAM1 = #ID,
#PARAM2 = 0
FETCH NEXT FROM c INTO #ID
END
CLOSE c; DEALLOCATE c;
You have two option here
Option 1 Using Split Function
Pass a comma deliminated list of IDs and use a Split function Inside your Procedure to make split these values and do whatever you want to do with it.
To
Make it work you will need two thing
1) Create a Function which
accepts a Comma Deliminated string and split them.
2) Modify you
Store Procedure and add this function in there in a way that passed
parameter is passed to the function inside that store procedure and
that function split the values before passing it onto your store
Procedure .
Create this function 1st
Function Definition
CREATE FUNCTION [dbo].[FnSplit]
(
#List nvarchar(2000),
#SplitOn nvarchar(5)
)
RETURNS #RtnValue table (Id int identity(1,1), Value nvarchar(100))
AS
BEGIN
WHILE(Charindex(#SplitOn,#List)>0)
BEGIN
INSERT INTO #RtnValue (value)
SELECT VALUE = ltrim(rtrim(Substring(#List,1,Charindex(#SplitOn,#List)-1)))
SET #List = SUBSTRING(#List,Charindex(#SplitOn,#List)+len(#SplitOn),len(#List))
END
INSERT INTO #RtnValue (Value)
SELECT VALUE = ltrim(rtrim(#List))
RETURN
END
Modify you strored Procedure something like this
Stored Procedure
ALTER Procedure [dbo].[spMYPROC] (#Param1 VARCHAR(1000)= NULL)
AS
BEGIN
SELECT * FROM TableName
where ColumnNAME IN (SELECT Value FROM dbo.FnSplit(#Param1,','))
END
GO
Option 2 Table Type Parameter
Create a Table Type and alter your proc to accept a Table Type Parameter and do whatever you want to do with them values inside your proc.
TABLE TYPE
CREATE TYPE dbo.TYPENAME AS TABLE
(
Value int
)
GO
Stored Procedure to Accept That Type Param
ALTER PROCEDURE [dbo].[spMYPROC]
#TableParam TYPENAME READONLY
AS
BEGIN
SET NOCOUNT ON;
--Temp table to store passed Id values
declare #tmp_values table (value INT );
--Insert passed values to a table variable inside the proc
INSERT INTO #tmp_values (value)
SELECT Value FROM #TableParam
/* Do your stuff here whatever you want to do with Ids */
END
EXECUTE PROC
Declare a variable of that type and populate it with your values.
DECLARE #Table TYPENAME --<-- Variable of this TYPE
INSERT INTO #Table --<-- Populating the variable
SELECT ID FROM myTABLE WHERE myName='bob'
EXECUTE [dbo].[spMYPROC] #Table --<-- Stored Procedure Executed

Resources