Solved

MSSQL: run stored proc with variable from select or list

Posted on 2014-04-11
3
353 Views
Last Modified: 2014-04-11
I would like to create one statement if possible. To process the following:
EXEC addAccount @accountNo = '123456'
EXEC addAccount @accountNo = 'ABCDEF'
EXEC addAccount @accountNo = '123ABC'
EXEC addAccount @accountNo = 'ABC123'


Like this:
EXEC addAccount @accountNo = ('123456', 'ABCDEF', '123ABC', 'ABC'123')
or
EXEC addAccount @accountNo = (SELECT accountNo FROM accountList WHERE new = 1)

I cannot modify the stored procedure.

Is this even possible?
0
Comment
Question by:rlarian
3 Comments
 
LVL 6

Accepted Solution

by:
Dulton earned 500 total points
ID: 39994577
Not with doing 1 EXEC. Since you can't alter the stored procedure's parameters, you can't send it more than 1 at a time.

As far as automation or making it dynamic, you can do something like:

DECLARE @AcctNbr VARCHAR(30)   --set the datatype and length accordingly
DECLARE @SqlString NVARCHAR(Max)
DECLARE @ParamDef NVARCHAR(Max)

declare AcctList CURSOR FOR
SELECT accountNo FROM accountList WHERE new = 1

Open AcctList

 FETCH NEXT FROM AcctList INTO @AcctNbr
     WHILE @@FETCHSTATUS = 0
         BEGIN
                  Select @SqlString = N'EXEC addAccount @accountNo'
                            ,@ParamDef = N'@accountNo VARCHAR(30)'         --set the datatype and length accordingly
               
            EXECUTE sp_executeSQL @StrSql, @ParamDef, @accountNo = @AcctNbr


              FETCH NEXT FROM AcctList INTO @AcctNbr
         END

CLOSE AcctList
DEALLOCATE AcctList

Open in new window

0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 39994588
You could create a table type and pass that as a variable.

CREATE TYPE dbo.IntList AS TABLE
(
   Value INT NOT NULL PRIMARY KEY CLUSTERED
);

Open in new window


Procedure:

CREATE PROCEDURE addAccount
(
   @AccountNoList IntList READONLY;
)
AS
...

Open in new window


Using Procedure:

DECLARE @AccountNoList IntList;

INSERT INTO @AccountNoList
SELECT accountNo FROM accountList WHERE new = 1

EXEC addAccount @AccountNoList = @AccountNoList

Open in new window

0
 
LVL 4

Author Closing Comment

by:rlarian
ID: 39994872
thanks!
0

Featured Post

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now