SQL SERVER动态获取字符串到临时表,及一对多的关系如何产生新表
问题1:变量不可以做为 in的条件。
DECLARE @Excludeddata varchar(100)='Qw,YY,TGC'
declare @Excludeddatatran varchar(100)=''''+replace(@ExcludedAirline,',',''',''')+''''
select @Excludeddatatran
'QF','YY','TG'
--IN 后面不能跟变量 无法识别
select * from 表名 where 字段1 IN (@Excludeddatatran )
select * from 表名 where 字段1 IN ('QF','YY','TG')
第一条select 找不到值,尴尬。
FIX:
create table #temp
(
code varchar(20)
)
declare @s varchar(100),@sql varchar(1000)
set @s='qw,vx,yy'
set @sql='select col='''+ replace(@s,',',''' union all select ''')+''''
PRINT @sql
insert into #temp
exec (@sql)
select * from 表名 where 字段1 in (select code from #temp)
问题2:两张不相关的表如何产生一对多的关系。
create table #tempa
(ID INT IDENTITY(1,1),
Code varchar(5),
description varchar(20),
fare money
)
insert into #tempa
values ('aaa','THIS IS A',500),('bbb','THIS IS B',200),('ccc','THIS IS C',100),('ddd','THIS IS D',20)
create table #tempb
(ID INT IDENTITY(1,1),
ProviderName VARCHAR(10),
)
INSERT INTO #tempb
VALUES ('HUAWEI'),('XIAOMI'),('PINGGUO')
SELECT * FROM #tempB
INNER JOIN #tempA ON 1=1
DROP TABLE #tempa
DROP TABLE #tempb
not negation of a word or group of words More (Definitions, Synonyms, Translation)

浙公网安备 33010602011771号