Ravi Ram
Ravi Ram

Reputation: 24488

Execute a stored procedure within a case statement

I have an Insert Into Select statement with a Case When clause. I want to execute a stored procedure within the When statement.

Insert into Orders(id, custId, custIntake)
Select id, custId custIntake = 
   Case
     When ( Exec mySProc(custId) = 1 ) = 'InStore'
     When ( Exec mySProc(custId) = 0 ) = 'OutsideStore'
     Else null
   End
From OrdersImport

How can I run Exec mySProc(custId) within the Case When?

Upvotes: 1

Views: 8421

Answers (3)

Steve
Steve

Reputation: 5545

I would suggest you convert your 'mySProc' procedure into a Scalar User Defined Function if you want to run it like this. Stored Procedures are not able to do what you want.

Upvotes: 3

Phoenix
Phoenix

Reputation: 1528

Do like this.

If you set auto increment for id column, not need to mention in the query. If you need, you can add it.

Insert into orders(custId, custIntake) 
Select custId, (CASE WHEN custId = '1' THEN 'InsideStore' ELSE 'OutsideStore' END)  from ordersimport;

Hope, it will help you.

Upvotes: 0

TomDillinger
TomDillinger

Reputation: 194

If I understand correctly then what you need is code to run when the WHEN statement is true. Just use CASE > WHEN > THEN as described here: http://msdn.microsoft.com/en-us/library/ms181765.aspx

Hope this helps.

Upvotes: 0

Related Questions