将许多行插入到另一列中具有相同字符串的一列中

时间:2013-12-18 09:13:46

标签: sql sql-server

我正在尝试让此查询正常工作

INSERT INTO [ImportedDateRange] ([DataSet],[DateRange]) 
'webshop', (select DISTINCT cast([OrderCreatedDate] as DATE) from webshop)

我基本上希望它看起来像这样:

DataSet     DateRange
webshop     01/10/2013
webshop     02/10/2013
webshop     03/10/2013
webshop     03/10/2013

每次输入webshop,但每个日期范围都会复制到新行。

还要检查DataSet网上商店的DateRange记录是否已存在

感谢您的帮助和建议

4 个答案:

答案 0 :(得分:2)

INSERT INTO [ImportedDateRange] ([DataSet],[DateRange]) 
SELECT DISTINCT  'webshop', CAST([OrderCreatedDate] as DATE) 
FROM   webshop

答案 1 :(得分:2)

要从webshop表的[ImportedDateRange]表中为DataSet网上商店插入唯一的DateRange记录,请写为:

INSERT INTO [ImportedDateRange] 
([DataSet],[DateRange]) 
select DISTINCT 'webshop', cast(T2.[OrderCreatedDate] as DATE) from webshop T2
WHERE NOT EXISTS (select 1 from [ImportedDateRange] T1 where T1.[DateRange] = T2.[OrderCreatedDate])

答案 2 :(得分:1)

INSERT INTO [ImportedDateRange] 
([DataSet],[DateRange]) 
select DISTINCT 'webshop', cast([OrderCreatedDate] as DATE) from webshop

答案 3 :(得分:1)

'webshop'应该在SELECT中,尝试自己运行select,因为输出将与插入 ImportedDateRange 表中的输出相同。

INSERT INTO [ImportedDateRange] 
([DataSet], [DateRange]) 
select DISTINCT 'webshop', cast([OrderCreatedDate] as DATE) from webshop;