| Author |
Topic |
|
SQLSoaker
Posting Yak Master
169 Posts |
Posted - 2008-12-22 : 11:55:02
|
| Hello,I have this SP (in short):ALTER PROCEDURE [dbo].[mySP] -- Add the parameters for the stored procedure here @ACCOUNTNUM VARCHAR(100), @DATAAREAID VARCHAR(100)ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; select b.Account as Account, b.balance as Balance, isnull(c.balance,0) as [Current], isnull(b1.balance,0) as [30 Day], isnull(b2.balance,0) as [60 Day], isnull(b3.balance,0) as [90 Day], isnull(b4.balance,0) as [120 Day], CASE b.Stopped_ WHEN '0' then 'No' WHEN '1' THEN 'Invoice' WHEN '2' THEN 'Yes' END as [Stop Status]from( select ctrans.accountnum, ctrans.dataareaid, sum(ctrans.amountcur - ctrans.settleamountcur) as Balance, custtable.name as Account, custtable.blocked as Stopped_from custtrans ctrans, custtablewhere ctrans.dataareaid = @DATAAREAID and ctrans.accountnum = @ACCOUNTNUM and ctrans.accountnum = custtable.accountnum and ctrans.dataareaid = custtable.dataareaidgroup by ctrans.accountnum, ctrans.dataareaid, custtable.name, custtable.blocked) bIs there someway I can call this SP with multiple @ACCOUNTNUM parameters and have the result set in ONE table???Something like this: (I know this doesnt work):exec my_SP @accountnum in ('1','2','3','4')Thank you in advance.Regards. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2008-12-24 : 03:57:21
|
| Also search for Array+SQL Server in googleMadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-25 : 02:21:03
|
| http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=115367 |
 |
|
|
SQLSoaker
Posting Yak Master
169 Posts |
Posted - 2008-12-29 : 16:47:22
|
| Okay so I have gone down this path:Create a function that parses thru the commas but I have another question: How do I add another parmater value to this function?:CREATE FUNCTION iter$simple_intlist_to_tbl (@list nvarchar(MAX)) RETURNS @tbl TABLE (number int NOT NULL) ASBEGIN DECLARE @pos int, @nextpos int, @valuelen int SELECT @pos = 0, @nextpos = 1 WHILE @nextpos > 0 BEGIN SELECT @nextpos = charindex(',', @list, @pos + 1) SELECT @valuelen = CASE WHEN @nextpos > 0 THEN @nextpos ELSE len(@list) + 1 END - @pos - 1 INSERT @tbl (number) VALUES (convert(int, substring(@list, @pos + 1, @valuelen))) SELECT @pos = @nextpos END RETURNENDYou normally run this as follows:EXEC sp_mySP '9, 12, 27, 37'But I want to run it as follows:EXEC sp_mySP '9, 12, 27, 37', @parm2 = 'xxx'How do I edit the function (iter$simple_intlist_to_tbl) so I can still call the sp with multiple paramters???Giving credit where due: http://www.sommarskog.se/arrays-in-sql-2005.htmlThank you in advance.Regards. |
 |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
SQLSoaker
Posting Yak Master
169 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
SQLSoaker
Posting Yak Master
169 Posts |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2008-12-29 : 18:49:26
|
| An alternate way is, in your original stored procedure, replace the section of the where clause that reads:ctrans.accountnum = @ACCOUNTNUMwith','+@ACCOUNTNUM+',' like '%,'+ctrans.accountnum+',%'Then, call the stored procedure like:exec my_SP @ ACCOUNTNUM = '1,2,3,7' |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 00:19:02
|
quote: Originally posted by SQLSoaker
quote: Originally posted by tkizer But why do you need it in the function?Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog
Because I cannot use it as follows:EXEC sp_mySP '9, 12, 27, 37', @parm2 = 'xxx'What I am doing is joining the function to the sp (sp_mySP) and then making a call to sp_mySP in another script.
why do you need @param2 in function? isntit just parsing comma seperated list? whats the purpose of param2 there? |
 |
|
|
SQLSoaker
Posting Yak Master
169 Posts |
Posted - 2008-12-30 : 14:02:22
|
quote: Originally posted by sunitabeck An alternate way is, in your original stored procedure, replace the section of the where clause that reads:ctrans.accountnum = @ACCOUNTNUMwith','+@ACCOUNTNUM+',' like '%,'+ctrans.accountnum+',%'Then, call the stored procedure like:exec my_SP @ ACCOUNTNUM = '1,2,3,7'
Thank you for the reply.I tried this but it is only picking up the first number in the sequence. In your example @ACCOUNTNUM = 1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 14:10:53
|
| nope it should work fine as long as you've only single avlues existing in accountnum field and you pass comma separated values through parameter. |
 |
|
|
SQLSoaker
Posting Yak Master
169 Posts |
Posted - 2008-12-30 : 14:11:45
|
This is what I am attempting (with a function)ALTER PROCEDURE [dbo].[sp_mySP] -- Add the parameters for the stored procedure here @ACCOUNTNUM VARCHAR(100), @DATAAREAID VARCHAR(100) ASBEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON;select b.Account as Account, b.accountnum_ as [Account Number], b.credit as [Credit Limit], b.balance as Balance, isnull(c.balance,0) as [Current], isnull(b1.balance,0) as [30 Day], isnull(b2.balance,0) as [60 Day], isnull(b3.balance,0) as [90 Day], isnull(b4.balance,0) as [120 Day], CASE b.Stopped_ WHEN '0' then 'No' WHEN '1' THEN 'Invoice' WHEN '2' THEN 'Yes' END as [Stop Status]from( select ctrans.accountnum, ctrans.dataareaid, sum(ctrans.amountcur - ctrans.settleamountcur) as Balance, custtable.name as Account, custtable.blocked as Stopped_, custtable.accountnum as accountnum_, custtable.creditmax as creditfrom custtrans ctrans, custtablewhere ctrans.dataareaid = @DATAAREAID and ctrans.accountnum = @ACCOUNTNUM and ctrans.accountnum = custtable.accountnum and ctrans.dataareaid = custtable.dataareaidgroup by ctrans.accountnum, ctrans.dataareaid, custtable.name, custtable.blocked, custtable.accountnum, custtable.creditmax) b --START MODjoin simple_intlist_to_tbl (@ACCOUNTNUM) i on b.accountnum_ = i.numbers--END MODTHE FUNCTIONset ANSI_NULLS ONset QUOTED_IDENTIFIER ONGOALTER FUNCTION [dbo].[simple_intlist_to_tbl] (@list nvarchar(MAX)) RETURNS @tbl TABLE (numbers int NOT NULL) ASBEGIN DECLARE @pos int, @nextpos int, @valuelen int SELECT @pos = 0, @nextpos = 1 WHILE @nextpos > 0 BEGIN SELECT @nextpos = charindex(',', @list, @pos + 1) SELECT @valuelen = CASE WHEN @nextpos > 0 THEN @nextpos ELSE len(@list) + 1 END - @pos - 1 INSERT @tbl (numbers) VALUES (convert(int, substring(@list, @pos + 1, @valuelen))) SELECT @pos = @nextpos END RETURNENDNOW THE CALL TO THE SP:exec sp_mySP '100124, 100144, 100253', @dataareaid = 'xxx'The result set is empty! Logically it should work! |
 |
|
|
SQLSoaker
Posting Yak Master
169 Posts |
Posted - 2008-12-30 : 14:13:50
|
quote: Originally posted by visakh16 nope it should work fine as long as you've only single avlues existing in accountnum field and you pass comma separated values through parameter.
How do I alter this with multiple values for the accountnum field! This field is a 6 digit number! |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 14:16:58
|
| does select * from simple_intlist_to_tbl (@ACCOUNTNUM) return you individual values correctly? |
 |
|
|
SQLSoaker
Posting Yak Master
169 Posts |
Posted - 2008-12-30 : 14:41:53
|
quote: Originally posted by visakh16 does select * from simple_intlist_to_tbl (@ACCOUNTNUM) return you individual values correctly?
Yes, select * from simple_intlist_to_tbl (10)returns a table with a value 10.select * from simple_intlist_to_tbl (10, 10)too many arguments..... |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-12-30 : 14:51:07
|
quote: Originally posted by SQLSoaker
quote: Originally posted by visakh16 does select * from simple_intlist_to_tbl (@ACCOUNTNUM) return you individual values correctly?
Yes, select * from simple_intlist_to_tbl (10)returns a table with a value 10.select * from simple_intlist_to_tbl (10, 10)too many arguments.....
for multiple values, pass it asselect * from simple_intlist_to_tbl ('10, 10') |
 |
|
|
SQLSoaker
Posting Yak Master
169 Posts |
Posted - 2008-12-30 : 16:30:03
|
Yes but I want multiple account numbers, meaning I need to pass @ACCOUNTNUM to the simple_intlist_to_tbl function and then use the , seperated "list" in the call to the SP!exec sp_mySP '1001, 1002, 1003', @DATAAREAID = 'xxx'I joined this line to my SP:join simple_intlist_to_tbl (@ACCOUNTNUM) i on b.accountnum_ = i.numbersShould I alter my SP to take away the @ACCOUNTNUM parameter so I can make calls to specific accounts???For example:join simple_intlist_to_tbl ('1001, 1002, 1003') i on b.accountnum_ = i.numbersquote: Originally posted by visakh16
quote: Originally posted by SQLSoaker
quote: Originally posted by visakh16 does select * from simple_intlist_to_tbl (@ACCOUNTNUM) return you individual values correctly?
Yes, select * from simple_intlist_to_tbl (10)returns a table with a value 10.select * from simple_intlist_to_tbl (10, 10)too many arguments.....
for multiple values, pass it asselect * from simple_intlist_to_tbl ('10, 10')
|
 |
|
|
SQLSoaker
Posting Yak Master
169 Posts |
Posted - 2008-12-30 : 16:31:17
|
visakh16 thank you!What I did was alter the SP and eliminated the @ACCOUNTNUM parameter and used specific accountnum's for it. This gave me my results all in one table!!!  |
 |
|
|
Next Page
|