添加 SQL 视图错误
Adding a SQL View Error
当我向 SQL 服务器添加视图时,我收到此错误:
Incorrect syntax near the keyword 'DECLARE'
因为我必须首先声明一个 tempTable Currency
然后从 Currecy
中插入一些值然后检索它并 Join
它到 Items
table Select
语句开始的地方
我的问题是,创建视图是否不允许创建
TempTable 或插入 ?
非常感谢。
视图不允许创建临时表。您需要使用存储过程
是的,视图只是一个 select 语句 - 它可能包含 CTE 或派生的 table。
如果您确实需要那些无法满足的过程逻辑,那么您可以使用多语句 table 值函数。
SQL 服务器定义 CREATE VIEW 如下:
CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ]
[ WITH <view_attribute> [ ,...n ] ]
AS select_statement
...
这意味着在 AS 之后您必须使用单个 select 语句。
如果愿意,您可以使用各种技术来避免填充您的临时 table。 CTE 是一种相对简单的技术,可以像临时工一样工作 table。
所以,而不是这个:
select *
into #tmp_currency
from Currency
...(alter table #tmp_currency)...
select *
from
othertable ot
join #tmp_currency tc
on tc.your_pk_col= ot.your_fk_col
...你可以使用这个...
;with tmp_currency as (
select
*,
new_col = (whatever value you want to calculate)
from Currency)
select *
from
othertable ot
join tmp_currency tc
on tc.your_pk_col= ot.your_fk_col
创建视图是一项微不足道的任务:
create view yourviewname as
with tmp_currency as (
select
*,
new_col = (whatever value you want to calculate)
from Currency)
select *
from
othertable ot
join tmp_currency tc
on tc.your_pk_col= ot.your_fk_col
当我向 SQL 服务器添加视图时,我收到此错误:
Incorrect syntax near the keyword 'DECLARE'
因为我必须首先声明一个 tempTable Currency
然后从 Currecy
中插入一些值然后检索它并 Join
它到 Items
table Select
语句开始的地方
我的问题是,创建视图是否不允许创建 TempTable 或插入 ?
非常感谢。
视图不允许创建临时表。您需要使用存储过程
是的,视图只是一个 select 语句 - 它可能包含 CTE 或派生的 table。
如果您确实需要那些无法满足的过程逻辑,那么您可以使用多语句 table 值函数。
SQL 服务器定义 CREATE VIEW 如下:
CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ]
[ WITH <view_attribute> [ ,...n ] ]
AS select_statement
...
这意味着在 AS 之后您必须使用单个 select 语句。 如果愿意,您可以使用各种技术来避免填充您的临时 table。 CTE 是一种相对简单的技术,可以像临时工一样工作 table。
所以,而不是这个:
select *
into #tmp_currency
from Currency
...(alter table #tmp_currency)...
select *
from
othertable ot
join #tmp_currency tc
on tc.your_pk_col= ot.your_fk_col
...你可以使用这个...
;with tmp_currency as (
select
*,
new_col = (whatever value you want to calculate)
from Currency)
select *
from
othertable ot
join tmp_currency tc
on tc.your_pk_col= ot.your_fk_col
创建视图是一项微不足道的任务:
create view yourviewname as
with tmp_currency as (
select
*,
new_col = (whatever value you want to calculate)
from Currency)
select *
from
othertable ot
join tmp_currency tc
on tc.your_pk_col= ot.your_fk_col