MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 22nd, 2004, 09:53 AM
rmhpirate rmhpirate is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Greenville, NC
Posts: 258 rmhpirate User rank is Corporal (100 - 500 Reputation Level)rmhpirate User rank is Corporal (100 - 500 Reputation Level)rmhpirate User rank is Corporal (100 - 500 Reputation Level)rmhpirate User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 7 h 8 m 49 sec
Reputation Power: 7
Question with IF..ELSE and Assignments in Transact SQL

I really need help with the following. I have a stored procedure that assigns values to declared variables and I need to make sure that I'm doing it right. It uses a lot of IF..ELSE statements which adds to the complexity, but here is a sample of code...

Code:
DECLARE @Code Int, @Weights Float

SELECT somestuff , @Code, @Weights

IF(something )
BEGIN
IF(something )
BEGIN
SELECT @Code = 2
SELECT @Weights = A*B 
END
END

FROM a bunch of tables 

WHERE a bunch of stuff 


Now the stored procedure is way more complicated than above, I just want to make sure I can assigne values to the declared variables the way I have done in the example above. Currently I get an error on the FROM line of the stored procedure.

Any help on this would be greately appreciated 'cause I have several people telling different things in regards to assigning variables in Transact-SQL. Thanks in advance

Reply With Quote
  #2  
Old December 22nd, 2004, 11:07 AM
elfy elfy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Poland
Posts: 11 elfy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 7 sec
Reputation Power: 0
Code:
DECLARE @Code Int, @Weights Float

IF( something )
  BEGIN
    IF( something )
      BEGIN
        SELECT @Code = 2
        SELECT @Weights = A*B 
      END
  END


This part is good and assign method for example "SELECT @Code = 2" is correct. I use this method in my every stored procedure.

Reply With Quote
  #3  
Old December 27th, 2004, 02:16 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,784 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 21 h 42 m 49 sec
Reputation Power: 37
You can't mix if and select that way.

Code:
if <condition>
begin
    select @x = ... from ...
    select @y = ... from ...
end
else
    select @z = ... from ...
end 

Reply With Quote
  #4  
Old December 28th, 2004, 09:08 AM
rmhpirate rmhpirate is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Greenville, NC
Posts: 258 rmhpirate User rank is Corporal (100 - 500 Reputation Level)rmhpirate User rank is Corporal (100 - 500 Reputation Level)rmhpirate User rank is Corporal (100 - 500 Reputation Level)rmhpirate User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 7 h 8 m 49 sec
Reputation Power: 7
What I've done (by reading some of the help files in T-SQL) is the following:

IF <condition>
BEGIN
SET @Code = 1
SET @Weights = 20
END

ELSE
IF <condition>
BEGIN
SET @Code = 2
END

IF<condition>
BEGIN
SET @Code = 3
SET @Weiths = 19
END
END

Even with this, I still get an error on the FROM line in the procedure.

It acts like it doesn't want to assign the values to the variables.

What I'm trying to do is create a dynamic query based on the values placed in the variables in the code.

If you have any idea of what's going on please let me know. I"m at my wits end on this one.

Reply With Quote
  #5  
Old December 28th, 2004, 06:05 PM
elfy elfy is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Location: Poland
Posts: 11 elfy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 7 sec
Reputation Power: 0
I don't understand why do you use IF inside "SELECT FROM " statement??? I think it is not allowed...

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Question with IF..ELSE and Assignments in Transact SQL


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway
Stay green...Green IT