|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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. |
|
#3
|
|||
|
|||
|
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
|
|
#4
|
|||
|
|||
|
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. |
|
#5
|
|||
|
|||
|
I don't understand why do you use IF inside "SELECT FROM " statement??? I think it is not allowed...
|
![]() |
| Viewing: Dev Shed Forums > Databases > MS SQL Development > Question with IF..ELSE and Assignments in Transact SQL |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|