为什么无法创建#Articole table
Why can't create the #Articole table
我尝试创建一个临时文件 table (#Articole) 但没有成功。哪里错了?
Declare @Articol int,
@Sql NVarchar(max)
set @Articol = 99190
set @Sql=N'
select im.ItemName, im.ItemCode, d.ItemId as PromoId, im.Enabled, im.ItemId, dfv.DiscountFilterValueId
into #Articol
from Discount d (nolock)
join DiscountFilter df (nolock) on df.DiscountId = d.ItemId
Join DiscountFilterValue dfv (nolock) on dfv.DiscountFilterId = df.DiscountFilterId
join Item i (nolock) on d.ItemId = i.ItemId
Join Item im (nolock) on im.ItemId = dfv.TextValue
where df.EntityId = 184'
IF @Articol <> -1 set @Sql = @Sql + ' and im.ItemId = @Articol'
If @Articol = -1 set @Sql = @Sql
exec sp_executesql @Sql, N'@Articol int', @Articol
select * from #Articole
EDIT 这里根本不需要动态 sql。这在您的原始查询中就足够了:and (@articol = -1 or im.ItemId = @articol)
.
临时 table 仅限于创建它的范围。您要么需要在 sp_executesql
之前创建它,要么将其余语句移动到动态查询字符串本身中,或者通过使用前缀 ##
而不是单个 [ 来使其成为全局临时 table =14=].
由于我不知道实际查询中 table 的架构,下面是一个预先创建它的技巧。
select top 0 im.ItemName, im.ItemCode, d.ItemId as PromoId, im.Enabled, im.ItemId, dfv.DiscountFilterValueId
into #Articol
from Discount d (nolock)
join DiscountFilter df (nolock) on df.DiscountId = d.ItemId
Join DiscountFilterValue dfv (nolock) on dfv.DiscountFilterId = df.DiscountFilterId
join Item i (nolock) on d.ItemId = i.ItemId
Join Item im (nolock) on im.ItemId = dfv.TextValue
where df.EntityId = 184
@Sql NVarchar(max)
set @Articol = 99190
set @Sql=N'
insert into #Articol
select im.ItemName, im.ItemCode, d.ItemId as PromoId, im.Enabled, im.ItemId, dfv.DiscountFilterValueId
from Discount d (nolock)
join DiscountFilter df (nolock) on df.DiscountId = d.ItemId
Join DiscountFilterValue dfv (nolock) on dfv.DiscountFilterId = df.DiscountFilterId
join Item i (nolock) on d.ItemId = i.ItemId
Join Item im (nolock) on im.ItemId = dfv.TextValue
where df.EntityId = 184'
IF @Articol <> -1 set @Sql = @Sql + ' and im.ItemId = @Articol'
If @Articol = -1 set @Sql = @Sql
exec sp_executesql @Sql, N'@Articol int', @Articol
select * from #Articole
试试这个:
CREATE TABLE #Articole (ItemName NVARCHAR(MAX), ItemCode INT, ItemId INT, Enabled BIT, SecondItemId INT, DiscountFilterValueId INT)
SET @Sql=N'
insert into #Articol
select im.ItemName, im.ItemCode, d.ItemId as PromoId, im.Enabled, im.ItemId, dfv.DiscountFilterValueId
from Discount d (nolock)
join DiscountFilter df (nolock) on df.DiscountId = d.ItemId
Join DiscountFilterValue dfv (nolock) on dfv.DiscountFilterId = df.DiscountFilterId
join Item i (nolock) on d.ItemId = i.ItemId
Join Item im (nolock) on im.ItemId = dfv.TextValue
where df.EntityId = 184'
IF @Articol <> -1 set @Sql = @Sql + ' and im.ItemId = @Articol'
If @Articol = -1 set @Sql = @Sql
exec sp_executesql @Sql, N'@Articol int', @Articol
select * from #Articole
我不知道你的专栏类型,我只是猜测。确保类型正确
我们可以在两个范围内创建临时 table,
- 本地范围 - 通过使用“#”声明创建,例如
#Articole
- 全局范围 - 通过使用“##”声明创建,例如
##Articole
要么在本地范围之外创建临时 Table,要么在全局范围内创建它。
我尝试创建一个临时文件 table (#Articole) 但没有成功。哪里错了?
Declare @Articol int,
@Sql NVarchar(max)
set @Articol = 99190
set @Sql=N'
select im.ItemName, im.ItemCode, d.ItemId as PromoId, im.Enabled, im.ItemId, dfv.DiscountFilterValueId
into #Articol
from Discount d (nolock)
join DiscountFilter df (nolock) on df.DiscountId = d.ItemId
Join DiscountFilterValue dfv (nolock) on dfv.DiscountFilterId = df.DiscountFilterId
join Item i (nolock) on d.ItemId = i.ItemId
Join Item im (nolock) on im.ItemId = dfv.TextValue
where df.EntityId = 184'
IF @Articol <> -1 set @Sql = @Sql + ' and im.ItemId = @Articol'
If @Articol = -1 set @Sql = @Sql
exec sp_executesql @Sql, N'@Articol int', @Articol
select * from #Articole
EDIT 这里根本不需要动态 sql。这在您的原始查询中就足够了:and (@articol = -1 or im.ItemId = @articol)
.
临时 table 仅限于创建它的范围。您要么需要在 sp_executesql
之前创建它,要么将其余语句移动到动态查询字符串本身中,或者通过使用前缀 ##
而不是单个 [ 来使其成为全局临时 table =14=].
由于我不知道实际查询中 table 的架构,下面是一个预先创建它的技巧。
select top 0 im.ItemName, im.ItemCode, d.ItemId as PromoId, im.Enabled, im.ItemId, dfv.DiscountFilterValueId
into #Articol
from Discount d (nolock)
join DiscountFilter df (nolock) on df.DiscountId = d.ItemId
Join DiscountFilterValue dfv (nolock) on dfv.DiscountFilterId = df.DiscountFilterId
join Item i (nolock) on d.ItemId = i.ItemId
Join Item im (nolock) on im.ItemId = dfv.TextValue
where df.EntityId = 184
@Sql NVarchar(max)
set @Articol = 99190
set @Sql=N'
insert into #Articol
select im.ItemName, im.ItemCode, d.ItemId as PromoId, im.Enabled, im.ItemId, dfv.DiscountFilterValueId
from Discount d (nolock)
join DiscountFilter df (nolock) on df.DiscountId = d.ItemId
Join DiscountFilterValue dfv (nolock) on dfv.DiscountFilterId = df.DiscountFilterId
join Item i (nolock) on d.ItemId = i.ItemId
Join Item im (nolock) on im.ItemId = dfv.TextValue
where df.EntityId = 184'
IF @Articol <> -1 set @Sql = @Sql + ' and im.ItemId = @Articol'
If @Articol = -1 set @Sql = @Sql
exec sp_executesql @Sql, N'@Articol int', @Articol
select * from #Articole
试试这个:
CREATE TABLE #Articole (ItemName NVARCHAR(MAX), ItemCode INT, ItemId INT, Enabled BIT, SecondItemId INT, DiscountFilterValueId INT)
SET @Sql=N'
insert into #Articol
select im.ItemName, im.ItemCode, d.ItemId as PromoId, im.Enabled, im.ItemId, dfv.DiscountFilterValueId
from Discount d (nolock)
join DiscountFilter df (nolock) on df.DiscountId = d.ItemId
Join DiscountFilterValue dfv (nolock) on dfv.DiscountFilterId = df.DiscountFilterId
join Item i (nolock) on d.ItemId = i.ItemId
Join Item im (nolock) on im.ItemId = dfv.TextValue
where df.EntityId = 184'
IF @Articol <> -1 set @Sql = @Sql + ' and im.ItemId = @Articol'
If @Articol = -1 set @Sql = @Sql
exec sp_executesql @Sql, N'@Articol int', @Articol
select * from #Articole
我不知道你的专栏类型,我只是猜测。确保类型正确
我们可以在两个范围内创建临时 table,
- 本地范围 - 通过使用“#”声明创建,例如
#Articole
- 全局范围 - 通过使用“##”声明创建,例如
##Articole
要么在本地范围之外创建临时 Table,要么在全局范围内创建它。