1
你好,我需要通過應用inner join
提取公共數據,這是在@sql
變量的每個循環中獲得的。我想對@sql
第一個循環獲得的結果應用inner join
到第二個等。多個動態選擇語句的內部連接
例如我已經設置值:
[email protected]@[email protected]#maal#saal,[email protected]@6nn#mm
上Attributes
variable`so它運行兩次,則可以運行多次。每個獲得的結果都應該是內部連接的,最終結果應該是存儲其他表。
DECLARE @Attributes NVARCHAR(4000)
SET @Attributes = '[email protected]@[email protected]#maal#saal,[email protected]@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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] 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 [email protected] AND (IsDeleted=0 OR IsDeleted IS NULL)'
END
SET @Counter = @Counter + 1
END
SELECT DISTINCT(條目標識號)FROM dbo.Aspx_ItemAttributesValue_Options WHERE的AttributeValue IN( '棉花')AND屬性Id = 5 AND ISNULL(請將isDeleted,0)= 0 INNER JOIN SELECT DISTINCT(ItemID)FROM dbo.Aspx_ItemAttributesValue_Options WHERE AttributeValue IN('Man')AND AttributeID = 6 AND ISNULL(IsDeleted,0)= 0.上面的代碼只返回兩個select語句,問題是我該如何加入動態選擇語句。 –
最後感謝我的解決方案...! –