Microsoft
Software
Hardware
Network
Question : Parsing multiple values (array) to a stored procedure
SQL SERVER 2005 & Reporting Services
Hi, I have a requirement to pass multiple values to a stored procedure. As I have found out there is no array type in T-SQL. Having googled it, there are a lot of resources out there with one variation or other but as I am not wonderfully sql orientated its difficult to try and transpose the various suggestions / examples for my own needs to get a working solution.
Basically I currently have a stored procedure that takes a parsed individual order no. It then determines how many lines are on the order and for every row on the order, performs a repeat of the following: determine the despatched qty and for every unit perform a second repeat to enter a duplicate line into a temp table from which the labels are then printed. Ie If my order has 5 lines all of qty 10 depsatched, then I get 10 identical lines inserted into the temp table for every row on the order (50 lines total in temp table).
This has been working fine but as always the requirement now is to parse multiple order nos (as selected by a Reporting Services report) to the procedure. This causes me my headache!!
The current procedure is as below: Can someone come up with a working solution that I do not have to interpet & significantly change.
As stated the order nos to be parsed will be generated from a reporting services report where the user will select multiple order nos from a drop down list of order nos in the system.
Thanks
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE procedure [scheme].[label_print] @Order_no char(10) as
DECLARE @order_no char (10)
-- Assign @order_no the parsed variable @Order_no
set @order_no = @Order_no
-- Calculate how many lines on the specified [parsed] order no.
DECLARE @ORDERLINECOUNT as INT;
set @ORDERLINECOUNT = (select count(1) from scheme.OrderDetails where LTRIM(order_no) = @order_no)
-- create temp table to receive the items to print.
CREATE TABLE [label_print_tmp] (
[customer] [char] (8) COLLATE Latin1_General_BIN NOT NULL ,
[name] [char] (32) COLLATE Latin1_General_BIN NOT NULL ,
[order_no] [char] (10) COLLATE Latin1_General_BIN NOT NULL ,
[order_line_no] [char] (4) COLLATE Latin1_General_BIN NOT NULL ,
[warehouse] [char] (2) COLLATE Latin1_General_BIN NOT NULL ,
[product] [char] (20) COLLATE Latin1_General_BIN NOT NULL ,
[description] [char] (20) COLLATE Latin1_General_BIN NOT NULL ,
[unit_of_sale] [char] (10) COLLATE Latin1_General_BIN NOT NULL ,
[despatched_qty] [float] NULL
) ON [PRIMARY]
-- Repeat next steps for every row on order.
While @ORDERLINECOUNT > 0
BEGIN
-- get line qty as labels required on a single unit basis (ie qty 10 requires 10 labels printed)
DECLARE @LINEQTYCOUNT as INT;
set @LINEQTYCOUNT = (select despatched_qty from scheme.OrderDetails where order_no = @order_no and LTRIM(order_line_no) = @ORDERLINECOUNT)
While @LINEQTYCOUNT > 0
BEGIN
insert into scheme.label_print_tmp select
scheme.OrderHeader.custome
r, scheme.OrderHeader.name,
scheme.OrderDetails.order_
no, scheme.OrderDetails.order_
line_no,
scheme.OrderDetails.wareho
use, scheme.OrderDetails.produc
t, scheme.OrderDetails.descri
ption,
scheme.OrderDetails.unit_o
f_sale, scheme.OrderDetails.despat
ched_qty
FROM scheme.OrderHeader INNER JOIN
scheme.OrderDetails ON scheme.OrderHeader.order_n
o = scheme.OrderDetails.order_
no INNER JOIN
scheme.Customer ON scheme.OrderHeader.custome
r = scheme.Customer.customer
where scheme.OrderDetails.order_
no =@order_no and LTRIM(order_line_no) = @ORDERLINECOUNT
and scheme.OrderDetails.line_t
ype != 'C'
set @LINEQTYCOUNT = @LINEQTYCOUNT -1
END
set @ORDERLINECOUNT=@ORDERLINE
COUNT -1;
END
select * from scheme.label_print_tmp
drop table scheme.label_print_tmp
Answer : Parsing multiple values (array) to a stored procedure
Scott,
Good point. I missed the 2nd loop variable.
Kyliem, please ignore my rewrite of your SP and just use the code snippet I posted for parsing a input string and calling your SP. Sorry for the incorrect info.
Thanks
Ray
Random Solutions
Reference a created movie clip, AS2 Flash 8
Data web programming on a $0 budget
SQL 2005, SSIS and Excel 2007
Excel 2007 Error: "can not find dll 'cgc.dll' to load!!"
Can a VBA function return an error code such as #DIV/0
How do you detach a movie clip when any action causes the movie to go to any other frame?
Traffic-Power Search Engine Placement - User Feedback?
Can I use a universal account with Acronis TrueImage Echo Workstation/Server?
Outlook and HTML Newsletter Background images
Exchange 5.5 MTA - error 2140: An internal windows NT error occurred