English 中文(简体)
服务器:使用TSQL变量的傲慢
原标题:SQL Server: Error while using TSQL variables

I have a user function that returns a BIT calle dbo.IsPartReady. I am trying to use the function inside of a trigger as follows:

SET @railReady = dbo.IsPartReady(1,@curPartiId);
SET @frameReady = dbo.IsPartReady(2,@curPartiId);
SET @dryAReady = dbo.IsPartReady(3,@curPartiId);
SET @dryBReady = dbo.IsPartReady(4,@curPartiId);

IF ( (@railReady AND @frameReady ) OR ( @dryAReady AND @dryBReady ) )

I m getting the following error in the IF statement: An expression of non-boolean type specified in a context where a condition is expected, near AND .

我做了什么错误?

最佳回答

BIT data type in SQL Server is not a boolean it is an integer. You have to compare the value of the variable with something to get a boolean expression. BIT can have the value 0, 1 or NULL. http://msdn.microsoft.com/en-us/library/ms177603.aspx

declare @B bit = 1

if @B = 1
begin
  print  Yes 
end
问题回答

使用:

IF ((@railReady = 1 AND @frameReady = 1) OR (@dryAReady = 1 AND @dryBReady = 1))

或替代

IF ((@railReady & @frameReady) | (@dryAReady & @dryBReady)) = 1

更多信息:

为了核实这一点,我们可以使用包含四条<代码>bit的所有组合的真相表。 价值:

WITH B(x) AS (SELECT CAST(0 AS bit) UNION ALL SELECT CAST(1 AS bit))
, AllSixteenCombinations(a,b,c,d) AS
    (SELECT * FROM B B1 CROSS JOIN B B2 CROSS JOIN B B3 CROSS JOIN B B4)
SELECT a,b,c,d
, CASE WHEN ((a = 1 AND b = 1) OR (c = 1 AND d = 1)) THEN  Y  ELSE  N  END[Logic]
, CASE WHEN ((a & b) | (c & d)) = 1 THEN  Y  ELSE  N  END [Bitwise]
FROM AllSixteenCombinations

产出:

a     b     c     d     Logic Bitwise
----- ----- ----- ----- ----- -------
0     0     0     0     N     N
0     1     0     0     N     N
0     0     1     0     N     N
0     1     1     0     N     N
1     0     0     0     N     N
1     1     0     0     Y     Y
1     0     1     0     N     N
1     1     1     0     Y     Y
0     0     0     1     N     N
0     1     0     1     N     N
0     0     1     1     Y     Y
0     1     1     1     Y     Y
1     0     0     1     N     N
1     1     0     1     Y     Y
1     0     1     1     Y     Y
1     1     1     1     Y     Y

(16 row(s) affected)




相关问题
How to write this T-SQL WHERE condition?

I ve got two tables: TableA Col1 Col2 TableB Col3 Col4 I want to join them together: SELECT * from TableA join TableB ON (...) Now, in place of ... I need to write an expression ...

Customer and Order Sql Statement

TSQL query to select all records from Customer that has an Order and also select all records from customer that does not have an Order. The table Customer contains a primary key of CustomerID. The ...

Recommended way of querying multiple Versioned tables

Have a win 2003 box with MSSQL 2005 running on it. There is a database which is populated every morning with new/modified SalesOrder made the previous day. The database has several tables: SalesOrder, ...

update duplicate record

I have a table with the following fields Id Name IsPublic i need to write a sql query that updates IsPublic to false where name has a duplicate. Only one of the duplicates should have IsPublic = ...

Define variable to use with IN operator (T-SQL)

I have a Transact-SQL query that uses the IN operator. Something like this: select * from myTable where myColumn in (1,2,3,4) Is there a way to define a variable to hold the entire list "(1,2,3,4)"? ...

Selecting records during recursive stored procedure

I ve got a content management system that contains a hierarchical structure of categories, with sub-categories subject to different ordering options at each level. Currently, that s retrieved by a (...

热门标签