日期:2014-05-17 浏览次数:20445 次
select * from 表A as a inner join 表B as b
on charindex(','+cast(b.id as varchar(20))+',',','+a.A+',')>0
Select
*
From a
Inner Join b On charindex(','+b.A'+',',','+a.A+',')>0
if exists (select 1
from sysobjects
where id = object_id('FN_GetTable')
and type in ('IF', 'FN', 'TF'))
drop function FN_GetTable
go
CREATE FUNCTION FN_GetTable(@COMM_STRING VARCHAR(8000))
RETURNS @TAB TABLE
(
ID VARCHAR(50)
)
WITH ENCRYPTION
AS
BEGIN
/*
将带逗号的字符串转换为行数据
入参:1,2,3
出参:可以查询的表
*/
DECLARE @SPLITCOUNT INT,
@I INT,
@VALUE_TEMP VARCHAR(50),
@VALUE_LIST_TEMP VARCHAR(8000),
@VALUESPLITINDEX INT
SET @I = 0
SET @VALUE_LIST_TEMP = LTRIM(RTRIM(@COMM_STRING))
IF LEN(@VALUE_LIST_TEMP) > 0
BEGIN
IF RIGHT(@VALUE_LIST_TEMP,1) <> ','
SET @VALUE_LIST_TEMP = @VALUE_LIST_TEMP + ',';
SET @SPLITCOUNT = LEN(@VALUE_LIST_TEMP) - LEN(REPLACE(@VALUE_LIST_TEMP,',','')) --分融符为','
WHILE (@I < @SPLITCOUNT)
BEGIN
SET @VALUESPLITINDEX = CHARINDEX(',',@VALUE_LIST_TEMP,0)
SET @VALUE_TEMP = SUBSTRING(@VALUE_LIST_TEMP,1,@VALUESPLITINDEX - 1)
IF LEN(LTRIM(RTRIM(@VALUE_TEMP))) > 0
INSERT INTO @TAB(ID) VALUES(@VALUE_TEMP)
SET @VALUE_LIST_TEMP = RIGHT(@VALUE_LIST_TEMP,LEN(@VALUE_LIST_TEMP) - @VALUESPLITINDEX)
SET @I = @I + 1
END
END
RETURN
END
go
SELECT *
FROM TEST
WHERE DISH_ID IN (SELECT *
FROM DBO.FN_GETTABLE ('10000001,10000002,10000003' ))