[求救]SQL SERVER执行SQL语句的时候的两个问题
1.SQLSERVER有组合主键的时候,在子查询中不可以返回多列结果吗。例如
select * from Table1 where (id,type) in (select id,type from table2)
现在我只能想到的解决方案就是
select * from Table1
where
(cast(id as varchar(10)) + type)
in (select cast(id as varchar(10)) + type from table2)
有其他的方式吗。
2.SQLSERVER
INSERT INTO [Role]
SELECT 1;
SELECT 2
上面insert报错的话。怎么让他继续执行下面的SELECT 2
= =求解释。。呜
[最优解释]
select * from Table1 where checksum(id,type) in (select checksum(id,type) from table2)
[其他解释]
第二个问题:
INSERT INTO [Role]
SELECT 1;
go
SELECT 2
[其他解释]
select * from Table1 where (id,type) in (select id,type from table2)
可以改成:
SELECT *
FROM table1 a
WHERE EXISTS ( SELECT 1
FROM ( SELECT id ,
type
FROM table2
) b
WHERE a.id = b.id
AND a.type = b.type )
SELECT * FROM table1 AS A WITH(NOLOCK)
WHERE EXISTS (SELECT 1 FROM table2 AS B WITH(NOLOCK) ON A.id=B.id AND A.[type]=B.[type])