• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 265
  • Last Modified:

Creating a Function in SQL Server 2008 - or Using an IF Statement

Hi,

I would like to put a bunch of code ive done in SQL into a function, which I would like to call. Example:

Function CallCode

......(this is where the code goes)
End Function

Case Code
 "202" then CallCode

(I know the above code is not right, but just want to explain how I want it done)

OR

IF Code = "202" Then
  CallCode
Endif

I want to do this in SQL - How can I do this?
0
AxleWack
Asked:
AxleWack
  • 2
  • 2
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
a "function" is something that returns a value, I presume you actually want to create a "procedure" instead ...

so, taking that you create a procedure

if @code = '202'
begin
  exec CallCode
end
0
 
aplusexpertCommented:
Hi,

If you want to conditional call for execute then follow below step:

If code = '1'
 exec Function1
else
 exec Function2
0
 
AxleWackAuthor Commented:
Thanks for the replies..... Ive decided to go a differant route, and created stored procedures for the 2 choices I would have had - I will then call these stored procedures in my code.

I have one more question..... if you could help please ?

I have 2 sets for data that I have pulled from tables I have.....

Now what I would like to do is update all fields in a table where the ID's in TABLE1 = all the same ID's in TABLE2

Here is the code ive done(but doesnt work due to the following error:)

Update Account
set IsActiveRec = 0
WHERE (Select MembershipID FROM Account) = (Select MembershipID FROM #CurBal)

ERROR:

Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

The error makes sense.... but how do I change the update query to do what I want?
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
Update Account
set IsActiveRec = 0
WHERE MembershipID in (Select MembershipID FROM #CurBal)
0
 
AxleWackAuthor Commented:
How simple..... thanks!!!!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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