首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sql with后只能跟select关键字吗?解决思路

2012-04-25 
sql with后只能跟select关键字吗?SQL code@zzxbgbit 1ASBEGINSET NOCOUNT ONWITH tb(Invoice_No, chxi,

sql with后只能跟select关键字吗?

SQL code
  @zzxbg       bit = 1ASBEGINSET NOCOUNT ON;    WITH tb(Invoice_No, chxi, ForAccountof, NotifyParty, Consignee, gs) AS    (     SELECT a.Invoice_No, a.chxi, c.ForAccountof, c.NotifyParty, c.Consignee, c.gs     from tb1    )   if zzxbg = 1   begin    ....   endEND

以上语句出错"关键字 'if' 附近有语法错误。"

请问with 后只能跟select关键字, 不能用if 语句是吗?

[解决办法]
应该是,可以建立临时表
[解决办法]
不能使用if。
[解决办法]
@zzxbg bit = 1
AS
BEGIN
SET NOCOUNT ON;
if zzxbg != 1
begin
WITH tb(Invoice_No, chxi, ForAccountof, NotifyParty, Consignee, gs) AS
(
SELECT a.Invoice_No, a.chxi, c.ForAccountof, c.NotifyParty, c.Consignee, c.gs
from tb1
)
end
if zzxbg = 1
begin
end



END


这样看可以不?
[解决办法]
SQL code
  @zzxbg       bit = 1ASBEGINSET NOCOUNT ON;    WITH tb(Invoice_No, chxi, ForAccountof, NotifyParty, Consignee, gs) AS    (     SELECT a.Invoice_No, a.chxi, c.ForAccountof, c.NotifyParty, c.Consignee, c.gs     from tb1    )--可以这样  select case @zzxbg when 1 then '' else '' endEND
[解决办法]
不是只能接select

是with 定义了表达式后马上要使用,例如:
SQL code
declare @t table(id int);with m as (select 1 as col)insert into @t select * from m
[解决办法]
定义公用表表达式后,须对其进行引用。 (语法限定)

热点排行
Bad Request.