您好,我需要通过应用在变量inner join
中的每个循环上获得的数据来提取公共数据。@sql
我想将inner join
第一个循环获得的结果应用于@sql
第二个循环,依此类推。
例如我设置了值:
1@3@4@kaal#maal#saal,2@5@6nn#mm
在Attributes
变量上运行两次,它可能会运行多次。获得的每个结果都应该是内部连接的,最终结果应该存储在其他表中。
DECLARE @Attributes NVARCHAR(4000)
SET @Attributes = '1@3@4@kaal#maal#saal,2@5@6nn#mm'
DECLARE @ItemAttributes NVARCHAR(4000)
DECLARE @tblAttributes TABLE (
RowNum INT identity(1, 1)
,Attribute NVARCHAR(1000)
)
INSERT INTO @tblAttributes (Attribute)
SELECT RTRIM(LTRIM(items))
FROM split(@Attributes, ',')
DECLARE @Counter INT
,@RowCount INT
DECLARE @sql NVARCHAR(2000)
SELECT @RowCount = count(RowNum)
FROM @tblAttributes
SET @Counter = 1
WHILE (
@Counter <= @RowCount
OR @Counter = 1
)
BEGIN
SELECT @ItemAttributes = Attribute
FROM @tblAttributes
WHERE RowNum = @Counter
DECLARE @tblAttr TABLE (
RowNum INT identity(1, 1)
,Attr NVARCHAR(1000)
)
INSERT INTO @tblAttr (Attr)
SELECT RTRIM(LTRIM(items))
FROM split(@ItemAttributes, '@')
DECLARE @AttributeID INT
DECLARE @InputTypeID INT
DECLARE @ValidationTypeID INT
DECLARE @AttributeValue NVARCHAR(4000)
SELECT @AttributeID = Attr
FROM @tblAttr
WHERE RowNum = 1
SELECT @InputTypeID = Attr
FROM @tblAttr
WHERE RowNum = 2
SELECT @ValidationTypeID = Attr
FROM @tblAttr
WHERE RowNum = 3
SELECT @AttributeValue = Attr
FROM @tblAttr
WHERE RowNum = 4
IF (@InputTypeID = 1)
BEGIN
IF (@ValidationTypeID = 3)
BEGIN
SELECT @sql = 'SELECT DISTINCT(ItemID) FROM dbo.Aspx_ItemAttributesValue_Decimal
WHERE AttributeValue IN(' + @AttributeValue + ') AND AttributeID =@AttributeID AND (IsDeleted=0 OR IsDeleted IS NULL)'
END
ELSE
IF (@ValidationTypeID = 5)
BEGIN
SELECT @sql = 'SELECT DISTINCT(ItemID) FROM dbo.Aspx_ItemAttributesValue_Int
WHERE AttributeValue IN(' + @AttributeValue + ') AND AttributeID =@AttributeID AND (IsDeleted=0 OR IsDeleted IS NULL)'
END
ELSE
BEGIN
SELECT @sql = 'SELECT DISTINCT(ItemID) FROM dbo.Aspx_ItemAttributesValue_Nvarchar
WHERE AttributeValue IN(' + @AttributeValue + ') AND AttributeID =@AttributeID AND (IsDeleted=0 OR IsDeleted IS NULL)'
END
END
IF (
@InputTypeID = 5
OR @InputTypeID = 6
OR @InputTypeID = 9
OR @InputTypeID = 10
OR @InputTypeID = 11
OR @InputTypeID = 12
)
BEGIN
SELECT @sql = 'SELECT DISTINCT(ItemID) FROM dbo.Aspx_ItemAttributesValue_Options
WHERE AttributeValue IN(' + @AttributeValue + ') AND AttributeID =@AttributeID AND (IsDeleted=0 OR IsDeleted IS NULL)'
END
IF (@InputTypeID = 7)
BEGIN
SELECT @sql = 'SELECT DISTINCT(ItemID) FROM dbo.Aspx_ItemAttributesValue_Decimal
WHERE AttributeValue IN(' + @AttributeValue + ') AND AttributeID =@AttributeID AND (IsDeleted=0 OR IsDeleted IS NULL)'
END
SET @Counter = @Counter + 1
END