Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 380
  • Last Modified:

MSSQL: run stored proc with variable from select or list

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
rlarian
Asked:
rlarian
1 Solution
 
DultonCommented:
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
 
Brian CroweCommented:
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
 
rlarianAuthor Commented:
thanks!
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now