Solved

MSSQL: run stored proc with variable from select or list

Posted on 2014-04-11
3
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

752 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