TSQL:select insert中的if语句

TSQL:select insert中的if语句,sql,tsql,function,if-statement,case,Sql,Tsql,Function,If Statement,Case,我在函数中有这样的结构: INSERT INTO @TheTable SELECT DISTINCT @Number AS Number, @Name AS Name FROM MYTABLE WHERE id = @id 我需要在这个结构中添加执行另一个函数的结果,我需要您的帮助 在@Name AS Name下面,我应该添加如下内容 IF (dbo.anotherFunction(@id)==1) @NewVisitor AS

我在函数中有这样的结构:

INSERT INTO @TheTable
    SELECT DISTINCT

        @Number AS Number,

        @Name AS Name

    FROM MYTABLE 
    WHERE id = @id
我需要在这个结构中添加执行另一个函数的结果,我需要您的帮助

在@Name AS Name下面,我应该添加如下内容

IF (dbo.anotherFunction(@id)==1) 
@NewVisitor AS NewVisitor
ELSE
@NewVisitor AS NoNewVisitor
如何将其转换为TSQL


万分感谢

猜这就是你想要的

INSERT INTO @TheTable
    SELECT DISTINCT
        @Number AS Number,
        @Name AS Name,
        case when (dbo.anotherFunction(@id)=1) then @NewVisitor else null end as NewVisitor,
        case when (dbo.anotherFunction(@id)<>1) then @NewVisitor else null end AS NoNewVisitor
    FROM MYTABLE 
    WHERE id = @id
插入@TheTable
选择不同的
@数字作为数字,
@姓名作为姓名,
当(dbo.anotherFunction(@id)=1)然后@NewVisitor else null结束为NewVisitor时,
(dbo.anotherFunction(@id)1)然后@NewVisitor else null结束为NoNewVisitor时的情况
从MYTABLE
其中id=@id

如果SELECT语句中没有可用的语句。相反,您需要利用一个案例


这个问题对我来说没有什么意义(为什么所有的变量,列的意思是什么?),但你可以这样做:

IF (dbo.anotherFunction(@id)==1) 
    INSERT INTO @TheTable
    SELECT DISTINCT
        @Number AS Number,
        @Name AS Name,
        @NewVisitor AS NewVisitor
    FROM MYTABLE 
    WHERE id = @id
ELSE
    INSERT INTO @TheTable
    SELECT DISTINCT
        @Number AS Number,
        @Name AS Name,
        @NewVisitor AS NoNewVisitor
    FROM MYTABLE 
    WHERE id = @id

或者对所选的NewVisitor和NoNewVisitor使用两个CASE语句。

这样您的
INSERT
就不会引用
MYTABLE
中的任何列,并且您会返回与
id
匹配的行数,然后除去除一行之外的所有行?如果存在(…)newVisitor和noNewVisitor是否在@TheTable中有两个不同的列,那么最好只使用
?那么你想把@newVisitor插入两列中的一列吗?这只是一个例子,请教我如何在该结构中使用if子句或类似的语句。非常感谢你会想要一份案例陈述,讨论是的,贝丝,就像你说的。非常感谢,这就是我需要的!!