在sql server上设置的存储过程

时间:2013-06-06 13:06:39

标签: sql-server tsql

我有选择数据的程序。

SELECT my_id FROM myDB WHERE @u_id = [u_id] 

现在我需要这样的东西:

if my_id = 1

set @anotherID = '10'

3 个答案:

答案 0 :(得分:0)

declare @my_Id int    
SELECT @my_Id = my_id FROM myDB WHERE @u_id = [u_id] 

if(@my_ID = 1)

BEGIN
set @anotherID = '10' 
END

答案 1 :(得分:0)

声明@anotherId int

选择@anotherId =(my_id = 1时的情况,然后是10,否则@anotherId结束)

来自@u_id = [u_id]

答案 2 :(得分:0)

你的问题很模糊,我不确定你真正想要的是什么,但尝试这样的事情:

--your existing code
SELECT my_id FROM myDB WHERE @u_id = [u_id] --returns a result set,
                                            -- of possibly many rows

--add this
if exists (select 1 from myDB WHERE @u_id = [u_id] AND my_id=1)
BEGIN
   SET @anotherID = '10' --hopefully you have declared this variable
                         --if @anotherID is a int/numeric data type 
                         --  you should just use: SET @anotherID = 10
                         --  notice the removed quotes
END