?
Solved

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

Posted on 2010-01-11
5
Medium Priority
?
261 Views
Last Modified: 2012-05-08
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
Comment
Question by:AxleWack
[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
  • 2
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 26281382
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
 
LVL 7

Expert Comment

by:aplusexpert
ID: 26281397
Hi,

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

If code = '1'
 exec Function1
else
 exec Function2
0
 
LVL 3

Author Comment

by:AxleWack
ID: 26281734
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
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 600 total points
ID: 26281745
Update Account
set IsActiveRec = 0
WHERE MembershipID in (Select MembershipID FROM #CurBal)
0
 
LVL 3

Author Comment

by:AxleWack
ID: 26281785
How simple..... thanks!!!!!
0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

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