在预期条件的上下文中指定的非布尔类型的表达式,接近' select'

时间:2016-04-01 16:42:43

标签: sql tsql azure-sql-database

我知道之前有人问过,但我认为答案是针对我的剧本的。这是我第一次创建proc并使用表变量。我正在解析一个表并从中为用户控件生成GridView。然后我需要使用用户控件上的复选框并转动它以更新原始表。 我需要的最后一部分是创建这个过程。

我确信有几种方法可以做到这一点,但这是一个Azure数据库,我非常接近完成它使这个选项工作。任何事都会有所帮助。

谢谢,

USE [OST]
GO

/****** Object:  StoredProcedure [mgt].[InsertPageSecurityMapping]    Script Date: 3/30/2016 11:30:55 AM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO


CREATE procedure [mgt].[UpdateAlerts] 
(   
    @pkDistro int
    ,@intAlerts int
    ,@vchrCustomer varchar(100)
)

as
begin

--create a table variable of the unpivoted mgt.tblDistro table 
DECLARE @unpvtDistroTemp TABLE
(
    pkDistroID int,
    vchrCustomer varchar(100),
    intAlerts int
)

INSERT INTO @unpvtDistroTemp (pkDistroID, vchrCustomer, intAlerts)
(
SELECT pkDistroID, vchrCustomer, intAlerts 
FROM 
(SELECT pkDistroID, intComcast, intCableVision, intHearst, intCharter FROM mgt.tblDistro) mtd 
UNPIVOT (intAlerts FOR vchrCustomer IN (intComcast, intCableVision, intHearst, intCharter)) as unpvtTable
)

--check to see if the alerts are a 1 or 0 and update the table
if @intAlerts = 1
begin
    UPDATE @unpvtDistroTemp
    SET intAlerts = 1
    WHERE pkDistroID = @pkDistro AND @vchrCustomer
select 1
end

if @intAlerts = 0
begin
    UPDATE @unpvtDistroTemp
    set intAlerts = 0 
    WHERE pkDistroID = @pkDistro AND vchrCustomer = @vchrCustomer
select 0    
end

UPDATE mgt.TblDistro 
SET @vchrCustomer = (SELECT @vchrCustomer
    FROM @unpvtDistroTemp
    PIVOT
    (MAX(intAlerts)
    FOR vchrCustomer IN (intComcast, intCableVision, intHearst, intCharter)) as pvtTable
    WHERE pkDistroID = @pkDistro)

end 



GO

1 个答案:

答案 0 :(得分:0)

Request