【问题标题】:CTE execute commands before using the CTE TableCTE 在使用 CTE 表之前执行命令
【发布时间】:2014-10-07 10:26:43
【问题描述】:

有没有办法在选择 CTE 表之前编写任何类型的代码?

DECLARE @TestTable TABLE (ID INT ,name NVARCHAR)
INSERT INTO @TestTable VALUES (1,'a'),(2,'b'),(1,'c')
;WITH TempCte(name)
AS
(
SELECT name FROM @TestTable WHERE ID = 1
)
PRINT 'test'
SELECT * FROM TempCte

【问题讨论】:

  • ; 需要放在语句的结尾,而不是中间的某个位置。
  • 你没有帮助。

标签: sql sql-server tsql


【解决方案1】:

不,那是不可能的。如果您参考 MSDN,那么:

A common table expression (CTE) can be thought of as a temporary result set 
that is defined within the execution scope of a SINGLE 
SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement.

所以基本上它是保存 CTE 结果集的 SINGLE SELECT/INSERT/UPDATE/DELETE/ 或 CREATE VIEW 语句的范围。在该范围之后编写的任何内容都将无法访问此临时数据。您可以在此处阅读更多信息: http://msdn.microsoft.com/en-us/library/ms175972.aspx

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-23
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    相关资源
    最近更新 更多