工会运作中的问题

时间:2016-12-29 06:50:17

标签: sql-server sql-server-2008 sql-server-2008-r2

我在数据库中有一个查询,如

  SELECT 0 AS [DocumentType],'Select Document Type' [DocumentTypeX],0 ,0
  UNION
  SELECT dbo.tbDocumentType.*
  FROM dbo.tbDocumentType where Site=@Site

它抛出错误消息“使用UNION,INTERSECT或EXCEPT运算符组合的所有查询必须在其目标列表中具有相同数量的表达式。”

2 个答案:

答案 0 :(得分:2)

UNION Operation的首要规则:

 1.Both Query should have the same number of the resultset.
 2.Respective Columns of both queries should have similar data types.
 3.Never go with TableName.*.Instead Specify Column Names

请检查......

答案 1 :(得分:1)

而不是

SELECT dbo.tbDocumentType.*

选择与UNION字段匹配的列

SELECT dbo.tbDocumentType.[DocumentType],
       dbo.tbDocumentType.[DocumentTypeX],
       dbo.tbDocumentType.[Something1],
       null  -- Or use any value you want if doesnt have the column