2016-05-26 141 views
0
CREATE SEQUENCE id AS INTEGER START WITH 1 INCREMENT BY 1; 

WITH source (Id, SomeColumn) AS 
(
      SELECT NEXT VALUE FOR id, 'some value 1' 
      UNION 
      SELECT NEXT VALUE FOR id, 'some value 2' 
) 
SELECT * FROM source; 

DROP SEQUENCE id; 

它引發以下錯誤:SQL Server將選擇

NEXT VALUE FOR function is not allowed in check constraints, default objects, computed columns, views, user-defined functions, user-defined aggregates, user-defined table types, sub-queries, common table expressions, derived tables or return statements.

爲什麼NEXT VALUE FOR功能不能與unions工作? 什麼是好的選擇?

我正在使用類似的CTE使用合併語句爲默認值的表建立種子。我試圖避免手動鍵入一系列數字。

我需要這樣的東西UNION SELECT ID ++

+0

這是如何「不工作」?請提供詳細信息。 –

+2

您是否考慮過ROW_NUMBER()窗口函數?你是否在尋找這樣的東西:http://stackoverflow.com/questions/37287657/sql-server-2012-create-a-new-table-with-instant-1000-record-that-c​​ontains-thre/37288556# 37288556? – Alex

+1

這種老式的解決方案適合你嗎? CREATE TABLE#時(ID INT IDENTITY(1,1),someValue中VARCHAR(255)) INSERT INTO#時(someValue中) SELECT '一些值1' AS someValue中 UNION SELECT '一些值2' SELECT * FROM #h H –

回答

0

根據Alex的評論和鏈接到這裏類似的問題是解決方案:

WITH source (SomeColumn) AS 
(
      SELECT 'some value 1' 
    UNION SELECT 'some value 2' 
) 
SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)), SomeColumn FROM source; 

以供將來參考:ROW_NUMBER() OVER (ORDER BY (SELECT NULL))是讓行號沒有辦法訂購一套