Как вставить несколько строк без повторения части инструкции INSERT INTO dbo.Blah? Sql insert несколько строк
sql - Код SQL для вставки нескольких строк в таблицу доступа ms
Вам нужна таблица, в которой будет содержаться хотя бы одна строка (лучше всего одна строка, и в этом случае вы можете опустить ключевое слово DISTINCT). Здесь я использую свою стандартную вспомогательную таблицу Calendar. В производной таблице DT1 вы должны указывать имена корреляции столбцов (разговорные "псевдонимы" ) только для первого появления:
INSERT INTO tblSimulation (p, cfYear, cfLocation, [Delta], Design, SigmaLoc, Sigma, SampleSize, Intercept) SELECT DT1.p, DT1.cfYear, DT1.cfLocation, DT1.Delta, DT1.Design, DT1.SigmaLoc, DT1.Sigma, DT1.SampleSize, DT1.Intercept FROM ( SELECT DISTINCT 0 AS p, 2 AS cfYear, 8.3 AS cfLocation, 0 AS Delta, 1 AS Design, 0.5 AS SigmaLoc, 0.2 AS Sigma, 220 AS SampleSize, 3.4 AS Intercept FROM Calendar UNION ALL SELECT DISTINCT 0, 2.4, 7.8, 0, 1, 0.5, 0.2, 220, 3.4 FROM Calendar UNION ALL SELECT DISTINCT 0, 2.3, 5.9, 0, 1, 0.5, 0.2, 220, 3.4 FROM Calendar ) AS DT1;Динамический SQL следует избегать в пользу хранимой процедуры, и так или иначе имена корреляции столбцов в производной таблице делают динамический SQL болью для такого типа конструкции. Итак, вот пример использования proc с параметрами 49 * 9 = 441 для создания до 49 строк в одном выполнении.
Кто-то спросил о производительности. Ну, этот proc создает 49 строк примерно за 100 миллисекунд на моей машине.
Схема:
CREATE TABLE OneRowTable ( lock CHAR(1) DEFAULT 'x' NOT NULL PRIMARY KEY, CONSTRAINT OneRowTable__lock CHECK (lock = 'x') ) ; INSERT INTO OneRowTable (lock) VALUES ('x') ; CREATE TABLE tblSimulation ( p DECIMAL(19,4), cfYear DECIMAL(19,4), cfLocation DECIMAL(19,4), Delta DECIMAL(19,4), Design DECIMAL(19,4), SigmaLoc DECIMAL(19,4), Sigma DECIMAL(19,4), SampleSize DECIMAL(19,4), Intercept DECIMAL(19,4) ) ; CREATE PROCEDURE Add100Simulations ( :001_1 DECIMAL(19, 4) = NULL, :001_2 DECIMAL(19, 4) = NULL, :001_3 DECIMAL(19, 4) = NULL, :001_4 DECIMAL(19, 4) = NULL, :001_5 DECIMAL(19, 4) = NULL, :001_6 DECIMAL(19, 4) = NULL, :001_7 DECIMAL(19, 4) = NULL, :001_8 DECIMAL(19, 4) = NULL, :001_9 DECIMAL(19, 4) = NULL, :002_1 DECIMAL(19, 4) = NULL, :002_2 DECIMAL(19, 4) = NULL, :002_3 DECIMAL(19, 4) = NULL, :002_4 DECIMAL(19, 4) = NULL, :002_5 DECIMAL(19, 4) = NULL, :002_6 DECIMAL(19, 4) = NULL, :002_7 DECIMAL(19, 4) = NULL, :002_8 DECIMAL(19, 4) = NULL, :002_9 DECIMAL(19, 4) = NULL, :003_1 DECIMAL(19, 4) = NULL, :003_2 DECIMAL(19, 4) = NULL, :003_3 DECIMAL(19, 4) = NULL, :003_4 DECIMAL(19, 4) = NULL, :003_5 DECIMAL(19, 4) = NULL, :003_6 DECIMAL(19, 4) = NULL, :003_7 DECIMAL(19, 4) = NULL, :003_8 DECIMAL(19, 4) = NULL, :003_9 DECIMAL(19, 4) = NULL, :004_1 DECIMAL(19, 4) = NULL, :004_2 DECIMAL(19, 4) = NULL, :004_3 DECIMAL(19, 4) = NULL, :004_4 DECIMAL(19, 4) = NULL, :004_5 DECIMAL(19, 4) = NULL, :004_6 DECIMAL(19, 4) = NULL, :004_7 DECIMAL(19, 4) = NULL, :004_8 DECIMAL(19, 4) = NULL, :004_9 DECIMAL(19, 4) = NULL, :005_1 DECIMAL(19, 4) = NULL, :005_2 DECIMAL(19, 4) = NULL, :005_3 DECIMAL(19, 4) = NULL, :005_4 DECIMAL(19, 4) = NULL, :005_5 DECIMAL(19, 4) = NULL, :005_6 DECIMAL(19, 4) = NULL, :005_7 DECIMAL(19, 4) = NULL, :005_8 DECIMAL(19, 4) = NULL, :005_9 DECIMAL(19, 4) = NULL, :006_1 DECIMAL(19, 4) = NULL, :006_2 DECIMAL(19, 4) = NULL, :006_3 DECIMAL(19, 4) = NULL, :006_4 DECIMAL(19, 4) = NULL, :006_5 DECIMAL(19, 4) = NULL, :006_6 DECIMAL(19, 4) = NULL, :006_7 DECIMAL(19, 4) = NULL, :006_8 DECIMAL(19, 4) = NULL, :006_9 DECIMAL(19, 4) = NULL, :007_1 DECIMAL(19, 4) = NULL, :007_2 DECIMAL(19, 4) = NULL, :007_3 DECIMAL(19, 4) = NULL, :007_4 DECIMAL(19, 4) = NULL, :007_5 DECIMAL(19, 4) = NULL, :007_6 DECIMAL(19, 4) = NULL, :007_7 DECIMAL(19, 4) = NULL, :007_8 DECIMAL(19, 4) = NULL, :007_9 DECIMAL(19, 4) = NULL, :008_1 DECIMAL(19, 4) = NULL, :008_2 DECIMAL(19, 4) = NULL, :008_3 DECIMAL(19, 4) = NULL, :008_4 DECIMAL(19, 4) = NULL, :008_5 DECIMAL(19, 4) = NULL, :008_6 DECIMAL(19, 4) = NULL, :008_7 DECIMAL(19, 4) = NULL, :008_8 DECIMAL(19, 4) = NULL, :008_9 DECIMAL(19, 4) = NULL, :009_1 DECIMAL(19, 4) = NULL, :009_2 DECIMAL(19, 4) = NULL, :009_3 DECIMAL(19, 4) = NULL, :009_4 DECIMAL(19, 4) = NULL, :009_5 DECIMAL(19, 4) = NULL, :009_6 DECIMAL(19, 4) = NULL, :009_7 DECIMAL(19, 4) = NULL, :009_8 DECIMAL(19, 4) = NULL, :009_9 DECIMAL(19, 4) = NULL, :010_1 DECIMAL(19, 4) = NULL, :010_2 DECIMAL(19, 4) = NULL, :010_3 DECIMAL(19, 4) = NULL, :010_4 DECIMAL(19, 4) = NULL, :010_5 DECIMAL(19, 4) = NULL, :010_6 DECIMAL(19, 4) = NULL, :010_7 DECIMAL(19, 4) = NULL, :010_8 DECIMAL(19, 4) = NULL, :010_9 DECIMAL(19, 4) = NULL, :011_1 DECIMAL(19, 4) = NULL, :011_2 DECIMAL(19, 4) = NULL, :011_3 DECIMAL(19, 4) = NULL, :011_4 DECIMAL(19, 4) = NULL, :011_5 DECIMAL(19, 4) = NULL, :011_6 DECIMAL(19, 4) = NULL, :011_7 DECIMAL(19, 4) = NULL, :011_8 DECIMAL(19, 4) = NULL, :011_9 DECIMAL(19, 4) = NULL, :012_1 DECIMAL(19, 4) = NULL, :012_2 DECIMAL(19, 4) = NULL, :012_3 DECIMAL(19, 4) = NULL, :012_4 DECIMAL(19, 4) = NULL, :012_5 DECIMAL(19, 4) = NULL, :012_6 DECIMAL(19, 4) = NULL, :012_7 DECIMAL(19, 4) = NULL, :012_8 DECIMAL(19, 4) = NULL, :012_9 DECIMAL(19, 4) = NULL, :013_1 DECIMAL(19, 4) = NULL, :013_2 DECIMAL(19, 4) = NULL, :013_3 DECIMAL(19, 4) = NULL, :013_4 DECIMAL(19, 4) = NULL, :013_5 DECIMAL(19, 4) = NULL, :013_6 DECIMAL(19, 4) = NULL, :013_7 DECIMAL(19, 4) = NULL, :013_8 DECIMAL(19, 4) = NULL, :013_9 DECIMAL(19, 4) = NULL, :014_1 DECIMAL(19, 4) = NULL, :014_2 DECIMAL(19, 4) = NULL, :014_3 DECIMAL(19, 4) = NULL, :014_4 DECIMAL(19, 4) = NULL, :014_5 DECIMAL(19, 4) = NULL, :014_6 DECIMAL(19, 4) = NULL, :014_7 DECIMAL(19, 4) = NULL, :014_8 DECIMAL(19, 4) = NULL, :014_9 DECIMAL(19, 4) = NULL, :015_1 DECIMAL(19, 4) = NULL, :015_2 DECIMAL(19, 4) = NULL, :015_3 DECIMAL(19, 4) = NULL, :015_4 DECIMAL(19, 4) = NULL, :015_5 DECIMAL(19, 4) = NULL, :015_6 DECIMAL(19, 4) = NULL, :015_7 DECIMAL(19, 4) = NULL, :015_8 DECIMAL(19, 4) = NULL, :015_9 DECIMAL(19, 4) = NULL, :016_1 DECIMAL(19, 4) = NULL, :016_2 DECIMAL(19, 4) = NULL, :016_3 DECIMAL(19, 4) = NULL, :016_4 DECIMAL(19, 4) = NULL, :016_5 DECIMAL(19, 4) = NULL, :016_6 DECIMAL(19, 4) = NULL, :016_7 DECIMAL(19, 4) = NULL, :016_8 DECIMAL(19, 4) = NULL, :016_9 DECIMAL(19, 4) = NULL, :017_1 DECIMAL(19, 4) = NULL, :017_2 DECIMAL(19, 4) = NULL, :017_3 DECIMAL(19, 4) = NULL, :017_4 DECIMAL(19, 4) = NULL, :017_5 DECIMAL(19, 4) = NULL, :017_6 DECIMAL(19, 4) = NULL, :017_7 DECIMAL(19, 4) = NULL, :017_8 DECIMAL(19, 4) = NULL, :017_9 DECIMAL(19, 4) = NULL, :018_1 DECIMAL(19, 4) = NULL, :018_2 DECIMAL(19, 4) = NULL, :018_3 DECIMAL(19, 4) = NULL, :018_4 DECIMAL(19, 4) = NULL, :018_5 DECIMAL(19, 4) = NULL, :018_6 DECIMAL(19, 4) = NULL, :018_7 DECIMAL(19, 4) = NULL, :018_8 DECIMAL(19, 4) = NULL, :018_9 DECIMAL(19, 4) = NULL, :019_1 DECIMAL(19, 4) = NULL, :019_2 DECIMAL(19, 4) = NULL, :019_3 DECIMAL(19, 4) = NULL, :019_4 DECIMAL(19, 4) = NULL, :019_5 DECIMAL(19, 4) = NULL, :019_6 DECIMAL(19, 4) = NULL, :019_7 DECIMAL(19, 4) = NULL, :019_8 DECIMAL(19, 4) = NULL, :019_9 DECIMAL(19, 4) = NULL, :020_1 DECIMAL(19, 4) = NULL, :020_2 DECIMAL(19, 4) = NULL, :020_3 DECIMAL(19, 4) = NULL, :020_4 DECIMAL(19, 4) = NULL, :020_5 DECIMAL(19, 4) = NULL, :020_6 DECIMAL(19, 4) = NULL, :020_7 DECIMAL(19, 4) = NULL, :020_8 DECIMAL(19, 4) = NULL, :020_9 DECIMAL(19, 4) = NULL, :021_1 DECIMAL(19, 4) = NULL, :021_2 DECIMAL(19, 4) = NULL, :021_3 DECIMAL(19, 4) = NULL, :021_4 DECIMAL(19, 4) = NULL, :021_5 DECIMAL(19, 4) = NULL, :021_6 DECIMAL(19, 4) = NULL, :021_7 DECIMAL(19, 4) = NULL, :021_8 DECIMAL(19, 4) = NULL, :021_9 DECIMAL(19, 4) = NULL, :022_1 DECIMAL(19, 4) = NULL, :022_2 DECIMAL(19, 4) = NULL, :022_3 DECIMAL(19, 4) = NULL, :022_4 DECIMAL(19, 4) = NULL, :022_5 DECIMAL(19, 4) = NULL, :022_6 DECIMAL(19, 4) = NULL, :022_7 DECIMAL(19, 4) = NULL, :022_8 DECIMAL(19, 4) = NULL, :022_9 DECIMAL(19, 4) = NULL, :023_1 DECIMAL(19, 4) = NULL, :023_2 DECIMAL(19, 4) = NULL, :023_3 DECIMAL(19, 4) = NULL, :023_4 DECIMAL(19, 4) = NULL, :023_5 DECIMAL(19, 4) = NULL, :023_6 DECIMAL(19, 4) = NULL, :023_7 DECIMAL(19, 4) = NULL, :023_8 DECIMAL(19, 4) = NULL, :023_9 DECIMAL(19, 4) = NULL, :024_1 DECIMAL(19, 4) = NULL, :024_2 DECIMAL(19, 4) = NULL, :024_3 DECIMAL(19, 4) = NULL, :024_4 DECIMAL(19, 4) = NULL, :024_5 DECIMAL(19, 4) = NULL, :024_6 DECIMAL(19, 4) = NULL, :024_7 DECIMAL(19, 4) = NULL, :024_8 DECIMAL(19, 4) = NULL, :024_9 DECIMAL(19, 4) = NULL, :025_1 DECIMAL(19, 4) = NULL, :025_2 DECIMAL(19, 4) = NULL, :025_3 DECIMAL(19, 4) = NULL, :025_4 DECIMAL(19, 4) = NULL, :025_5 DECIMAL(19, 4) = NULL, :025_6 DECIMAL(19, 4) = NULL, :025_7 DECIMAL(19, 4) = NULL, :025_8 DECIMAL(19, 4) = NULL, :025_9 DECIMAL(19, 4) = NULL, :026_1 DECIMAL(19, 4) = NULL, :026_2 DECIMAL(19, 4) = NULL, :026_3 DECIMAL(19, 4) = NULL, :026_4 DECIMAL(19, 4) = NULL, :026_5 DECIMAL(19, 4) = NULL, :026_6 DECIMAL(19, 4) = NULL, :026_7 DECIMAL(19, 4) = NULL, :026_8 DECIMAL(19, 4) = NULL, :026_9 DECIMAL(19, 4) = NULL, :027_1 DECIMAL(19, 4) = NULL, :027_2 DECIMAL(19, 4) = NULL, :027_3 DECIMAL(19, 4) = NULL, :027_4 DECIMAL(19, 4) = NULL, :027_5 DECIMAL(19, 4) = NULL, :027_6 DECIMAL(19, 4) = NULL, :027_7 DECIMAL(19, 4) = NULL, :027_8 DECIMAL(19, 4) = NULL, :027_9 DECIMAL(19, 4) = NULL, :028_1 DECIMAL(19, 4) = NULL, :028_2 DECIMAL(19, 4) = NULL, :028_3 DECIMAL(19, 4) = NULL, :028_4 DECIMAL(19, 4) = NULL, :028_5 DECIMAL(19, 4) = NULL, :028_6 DECIMAL(19, 4) = NULL, :028_7 DECIMAL(19, 4) = NULL, :028_8 DECIMAL(19, 4) = NULL, :028_9 DECIMAL(19, 4) = NULL, :029_1 DECIMAL(19, 4) = NULL, :029_2 DECIMAL(19, 4) = NULL, :029_3 DECIMAL(19, 4) = NULL, :029_4 DECIMAL(19, 4) = NULL, :029_5 DECIMAL(19, 4) = NULL, :029_6 DECIMAL(19, 4) = NULL, :029_7 DECIMAL(19, 4) = NULL, :029_8 DECIMAL(19, 4) = NULL, :029_9 DECIMAL(19, 4) = NULL, :030_1 DECIMAL(19, 4) = NULL, :030_2 DECIMAL(19, 4) = NULL, :030_3 DECIMAL(19, 4) = NULL, :030_4 DECIMAL(19, 4) = NULL, :030_5 DECIMAL(19, 4) = NULL, :030_6 DECIMAL(19, 4) = NULL, :030_7 DECIMAL(19, 4) = NULL, :030_8 DECIMAL(19, 4) = NULL, :030_9 DECIMAL(19, 4) = NULL, :031_1 DECIMAL(19, 4) = NULL, :031_2 DECIMAL(19, 4) = NULL, :031_3 DECIMAL(19, 4) = NULL, :031_4 DECIMAL(19, 4) = NULL, :031_5 DECIMAL(19, 4) = NULL, :031_6 DECIMAL(19, 4) = NULL, :031_7 DECIMAL(19, 4) = NULL, :031_8 DECIMAL(19, 4) = NULL, :031_9 DECIMAL(19, 4) = NULL, :032_1 DECIMAL(19, 4) = NULL, :032_2 DECIMAL(19, 4) = NULL, :032_3 DECIMAL(19, 4) = NULL, :032_4 DECIMAL(19, 4) = NULL, :032_5 DECIMAL(19, 4) = NULL, :032_6 DECIMAL(19, 4) = NULL, :032_7 DECIMAL(19, 4) = NULL, :032_8 DECIMAL(19, 4) = NULL, :032_9 DECIMAL(19, 4) = NULL, :033_1 DECIMAL(19, 4) = NULL, :033_2 DECIMAL(19, 4) = NULL, :033_3 DECIMAL(19, 4) = NULL, :033_4 DECIMAL(19, 4) = NULL, :033_5 DECIMAL(19, 4) = NULL, :033_6 DECIMAL(19, 4) = NULL, :033_7 DECIMAL(19, 4) = NULL, :033_8 DECIMAL(19, 4) = NULL, :033_9 DECIMAL(19, 4) = NULL, :034_1 DECIMAL(19, 4) = NULL, :034_2 DECIMAL(19, 4) = NULL, :034_3 DECIMAL(19, 4) = NULL, :034_4 DECIMAL(19, 4) = NULL, :034_5 DECIMAL(19, 4) = NULL, :034_6 DECIMAL(19, 4) = NULL, :034_7 DECIMAL(19, 4) = NULL, :034_8 DECIMAL(19, 4) = NULL, :034_9 DECIMAL(19, 4) = NULL, :035_1 DECIMAL(19, 4) = NULL, :035_2 DECIMAL(19, 4) = NULL, :035_3 DECIMAL(19, 4) = NULL, :035_4 DECIMAL(19, 4) = NULL, :035_5 DECIMAL(19, 4) = NULL, :035_6 DECIMAL(19, 4) = NULL, :035_7 DECIMAL(19, 4) = NULL, :035_8 DECIMAL(19, 4) = NULL, :035_9 DECIMAL(19, 4) = NULL, :036_1 DECIMAL(19, 4) = NULL, :036_2 DECIMAL(19, 4) = NULL, :036_3 DECIMAL(19, 4) = NULL, :036_4 DECIMAL(19, 4) = NULL, :036_5 DECIMAL(19, 4) = NULL, :036_6 DECIMAL(19, 4) = NULL, :036_7 DECIMAL(19, 4) = NULL, :036_8 DECIMAL(19, 4) = NULL, :036_9 DECIMAL(19, 4) = NULL, :037_1 DECIMAL(19, 4) = NULL, :037_2 DECIMAL(19, 4) = NULL, :037_3 DECIMAL(19, 4) = NULL, :037_4 DECIMAL(19, 4) = NULL, :037_5 DECIMAL(19, 4) = NULL, :037_6 DECIMAL(19, 4) = NULL, :037_7 DECIMAL(19, 4) = NULL, :037_8 DECIMAL(19, 4) = NULL, :037_9 DECIMAL(19, 4) = NULL, :038_1 DECIMAL(19, 4) = NULL, :038_2 DECIMAL(19, 4) = NULL, :038_3 DECIMAL(19, 4) = NULL, :038_4 DECIMAL(19, 4) = NULL, :038_5 DECIMAL(19, 4) = NULL, :038_6 DECIMAL(19, 4) = NULL, :038_7 DECIMAL(19, 4) = NULL, :038_8 DECIMAL(19, 4) = NULL, :038_9 DECIMAL(19, 4) = NULL, :039_1 DECIMAL(19, 4) = NULL, :039_2 DECIMAL(19, 4) = NULL, :039_3 DECIMAL(19, 4) = NULL, :039_4 DECIMAL(19, 4) = NULL, :039_5 DECIMAL(19, 4) = NULL, :039_6 DECIMAL(19, 4) = NULL, :039_7 DECIMAL(19, 4) = NULL, :039_8 DECIMAL(19, 4) = NULL, :039_9 DECIMAL(19, 4) = NULL, :040_1 DECIMAL(19, 4) = NULL, :040_2 DECIMAL(19, 4) = NULL, :040_3 DECIMAL(19, 4) = NULL, :040_4 DECIMAL(19, 4) = NULL, :040_5 DECIMAL(19, 4) = NULL, :040_6 DECIMAL(19, 4) = NULL, :040_7 DECIMAL(19, 4) = NULL, :040_8 DECIMAL(19, 4) = NULL, :040_9 DECIMAL(19, 4) = NULL, :041_1 DECIMAL(19, 4) = NULL, :041_2 DECIMAL(19, 4) = NULL, :041_3 DECIMAL(19, 4) = NULL, :041_4 DECIMAL(19, 4) = NULL, :041_5 DECIMAL(19, 4) = NULL, :041_6 DECIMAL(19, 4) = NULL, :041_7 DECIMAL(19, 4) = NULL, :041_8 DECIMAL(19, 4) = NULL, :041_9 DECIMAL(19, 4) = NULL, :042_1 DECIMAL(19, 4) = NULL, :042_2 DECIMAL(19, 4) = NULL, :042_3 DECIMAL(19, 4) = NULL, :042_4 DECIMAL(19, 4) = NULL, :042_5 DECIMAL(19, 4) = NULL, :042_6 DECIMAL(19, 4) = NULL, :042_7 DECIMAL(19, 4) = NULL, :042_8 DECIMAL(19, 4) = NULL, :042_9 DECIMAL(19, 4) = NULL, :043_1 DECIMAL(19, 4) = NULL, :043_2 DECIMAL(19, 4) = NULL, :043_3 DECIMAL(19, 4) = NULL, :043_4 DECIMAL(19, 4) = NULL, :043_5 DECIMAL(19, 4) = NULL, :043_6 DECIMAL(19, 4) = NULL, :043_7 DECIMAL(19, 4) = NULL, :043_8 DECIMAL(19, 4) = NULL, :043_9 DECIMAL(19, 4) = NULL, :044_1 DECIMAL(19, 4) = NULL, :044_2 DECIMAL(19, 4) = NULL, :044_3 DECIMAL(19, 4) = NULL, :044_4 DECIMAL(19, 4) = NULL, :044_5 DECIMAL(19, 4) = NULL, :044_6 DECIMAL(19, 4) = NULL, :044_7 DECIMAL(19, 4) = NULL, :044_8 DECIMAL(19, 4) = NULL, :044_9 DECIMAL(19, 4) = NULL, :045_1 DECIMAL(19, 4) = NULL, :045_2 DECIMAL(19, 4) = NULL, :045_3 DECIMAL(19, 4) = NULL, :045_4 DECIMAL(19, 4) = NULL, :045_5 DECIMAL(19, 4) = NULL, :045_6 DECIMAL(19, 4) = NULL, :045_7 DECIMAL(19, 4) = NULL, :045_8 DECIMAL(19, 4) = NULL, :045_9 DECIMAL(19, 4) = NULL, :046_1 DECIMAL(19, 4) = NULL, :046_2 DECIMAL(19, 4) = NULL, :046_3 DECIMAL(19, 4) = NULL, :046_4 DECIMAL(19, 4) = NULL, :046_5 DECIMAL(19, 4) = NULL, :046_6 DECIMAL(19, 4) = NULL, :046_7 DECIMAL(19, 4) = NULL, :046_8 DECIMAL(19, 4) = NULL, :046_9 DECIMAL(19, 4) = NULL, :047_1 DECIMAL(19, 4) = NULL, :047_2 DECIMAL(19, 4) = NULL, :047_3 DECIMAL(19, 4) = NULL, :047_4 DECIMAL(19, 4) = NULL, :047_5 DECIMAL(19, 4) = NULL, :047_6 DECIMAL(19, 4) = NULL, :047_7 DECIMAL(19, 4) = NULL, :047_8 DECIMAL(19, 4) = NULL, :047_9 DECIMAL(19, 4) = NULL, :048_1 DECIMAL(19, 4) = NULL, :048_2 DECIMAL(19, 4) = NULL, :048_3 DECIMAL(19, 4) = NULL, :048_4 DECIMAL(19, 4) = NULL, :048_5 DECIMAL(19, 4) = NULL, :048_6 DECIMAL(19, 4) = NULL, :048_7 DECIMAL(19, 4) = NULL, :048_8 DECIMAL(19, 4) = NULL, :048_9 DECIMAL(19, 4) = NULL, :049_1 DECIMAL(19, 4) = NULL, :049_2 DECIMAL(19, 4) = NULL, :049_3 DECIMAL(19, 4) = NULL, :049_4 DECIMAL(19, 4) = NULL, :049_5 DECIMAL(19, 4) = NULL, :049_6 DECIMAL(19, 4) = NULL, :049_7 DECIMAL(19, 4) = NULL, :049_8 DECIMAL(19, 4) = NULL, :049_9 DECIMAL(19, 4) = NULL ) AS INSERT INTO tblSimulation (p, cfYear, cfLocation, [Delta], Design, SigmaLoc, Sigma, SampleSize, Intercept) SELECT DT1.p, DT1.cfYear, DT1.cfLocation, DT1.Delta, DT1.Design, DT1.SigmaLoc, DT1.Sigma, DT1.SampleSize, DT1.Intercept FROM ( SELECT :001_1 AS p, :001_2 AS cfYear, :001_3 AS cfLocation, :001_4 AS Delta, :001_5 AS Design, :001_6 AS SigmaLoc, :001_7 AS Sigma, :001_8 AS SampleSize, :001_9 AS Intercept FROM OneRowTable UNION ALL SELECT :002_1, :002_2, :002_3, :002_4, :002_5, :002_6, :002_7, :002_8, :002_9 FROM OneRowTable UNION ALL SELECT :003_1, :003_2, :003_3, :003_4, :003_5, :003_6, :003_7, :003_8, :003_9 FROM OneRowTable UNION ALL SELECT :004_1, :004_2, :004_3, :004_4, :004_5, :004_6, :004_7, :004_8, :004_9 FROM OneRowTable UNION ALL SELECT :005_1, :005_2, :005_3, :005_4, :005_5, :005_6, :005_7, :005_8, :005_9 FROM OneRowTable UNION ALL SELECT :006_1, :006_2, :006_3, :006_4, :006_5, :006_6, :006_7, :006_8, :006_9 FROM OneRowTable UNION ALL SELECT :007_1, :007_2, :007_3, :007_4, :007_5, :007_6, :007_7, :007_8, :007_9 FROM OneRowTable UNION ALL SELECT :008_1, :008_2, :008_3, :008_4, :008_5, :008_6, :008_7, :008_8, :008_9 FROM OneRowTable UNION ALL SELECT :009_1, :009_2, :009_3, :009_4, :009_5, :009_6, :009_7, :009_8, :009_9 FROM OneRowTable UNION ALL SELECT :010_1, :010_2, :010_3, :010_4, :010_5, :010_6, :010_7, :010_8, :010_9 FROM OneRowTable UNION ALL SELECT :011_1, :011_2, :011_3, :011_4, :011_5, :011_6, :011_7, :011_8, :011_9 FROM OneRowTable UNION ALL SELECT :012_1, :012_2, :012_3, :012_4, :012_5, :012_6, :012_7, :012_8, :012_9 FROM OneRowTable UNION ALL SELECT :013_1, :013_2, :013_3, :013_4, :013_5, :013_6, :013_7, :013_8, :013_9 FROM OneRowTable UNION ALL SELECT :014_1, :014_2, :014_3, :014_4, :014_5, :014_6, :014_7, :014_8, :014_9 FROM OneRowTable UNION ALL SELECT :015_1, :015_2, :015_3, :015_4, :015_5, :015_6, :015_7, :015_8, :015_9 FROM OneRowTable UNION ALL SELECT :016_1, :016_2, :016_3, :016_4, :016_5, :016_6, :016_7, :016_8, :016_9 FROM OneRowTable UNION ALL SELECT :017_1, :017_2, :017_3, :017_4, :017_5, :017_6, :017_7, :017_8, :017_9 FROM OneRowTable UNION ALL SELECT :018_1, :018_2, :018_3, :018_4, :018_5, :018_6, :018_7, :018_8, :018_9 FROM OneRowTable UNION ALL SELECT :019_1, :019_2, :019_3, :019_4, :019_5, :019_6, :019_7, :019_8, :019_9 FROM OneRowTable UNION ALL SELECT :020_1, :020_2, :020_3, :020_4, :020_5, :020_6, :020_7, :020_8, :020_9 FROM OneRowTable UNION ALL SELECT :021_1, :021_2, :021_3, :021_4, :021_5, :021_6, :021_7, :021_8, :021_9 FROM OneRowTable UNION ALL SELECT :022_1, :022_2, :022_3, :022_4, :022_5, :022_6, :022_7, :022_8, :022_9 FROM OneRowTable UNION ALL SELECT :023_1, :023_2, :023_3, :023_4, :023_5, :023_6, :023_7, :023_8, :023_9 FROM OneRowTable UNION ALL SELECT :024_1, :024_2, :024_3, :024_4, :024_5, :024_6, :024_7, :024_8, :024_9 FROM OneRowTable UNION ALL SELECT :025_1, :025_2, :025_3, :025_4, :025_5, :025_6, :025_7, :025_8, :025_9 FROM OneRowTable UNION ALL SELECT :026_1, :026_2, :026_3, :026_4, :026_5, :026_6, :026_7, :026_8, :026_9 FROM OneRowTable UNION ALL SELECT :027_1, :027_2, :027_3, :027_4, :027_5, :027_6, :027_7, :027_8, :027_9 FROM OneRowTable UNION ALL SELECT :028_1, :028_2, :028_3, :028_4, :028_5, :028_6, :028_7, :028_8, :028_9 FROM OneRowTable UNION ALL SELECT :029_1, :029_2, :029_3, :029_4, :029_5, :029_6, :029_7, :029_8, :029_9 FROM OneRowTable UNION ALL SELECT :030_1, :030_2, :030_3, :030_4, :030_5, :030_6, :030_7, :030_8, :030_9 FROM OneRowTable UNION ALL SELECT :031_1, :031_2, :031_3, :031_4, :031_5, :031_6, :031_7, :031_8, :031_9 FROM OneRowTable UNION ALL SELECT :032_1, :032_2, :032_3, :032_4, :032_5, :032_6, :032_7, :032_8, :032_9 FROM OneRowTable UNION ALL SELECT :033_1, :033_2, :033_3, :033_4, :033_5, :033_6, :033_7, :033_8, :033_9 FROM OneRowTable UNION ALL SELECT :034_1, :034_2, :034_3, :034_4, :034_5, :034_6, :034_7, :034_8, :034_9 FROM OneRowTable UNION ALL SELECT :035_1, :035_2, :035_3, :035_4, :035_5, :035_6, :035_7, :035_8, :035_9 FROM OneRowTable UNION ALL SELECT :036_1, :036_2, :036_3, :036_4, :036_5, :036_6, :036_7, :036_8, :036_9 FROM OneRowTable UNION ALL SELECT :037_1, :037_2, :037_3, :037_4, :037_5, :037_6, :037_7, :037_8, :037_9 FROM OneRowTable UNION ALL SELECT :038_1, :038_2, :038_3, :038_4, :038_5, :038_6, :038_7, :038_8, :038_9 FROM OneRowTable UNION ALL SELECT :039_1, :039_2, :039_3, :039_4, :039_5, :039_6, :039_7, :039_8, :039_9 FROM OneRowTable UNION ALL SELECT :040_1, :040_2, :040_3, :040_4, :040_5, :040_6, :040_7, :040_8, :040_9 FROM OneRowTable UNION ALL SELECT :041_1, :041_2, :041_3, :041_4, :041_5, :041_6, :041_7, :041_8, :041_9 FROM OneRowTable UNION ALL SELECT :042_1, :042_2, :042_3, :042_4, :042_5, :042_6, :042_7, :042_8, :042_9 FROM OneRowTable UNION ALL SELECT :043_1, :043_2, :043_3, :043_4, :043_5, :043_6, :043_7, :043_8, :043_9 FROM OneRowTable UNION ALL SELECT :044_1, :044_2, :044_3, :044_4, :044_5, :044_6, :044_7, :044_8, :044_9 FROM OneRowTable UNION ALL SELECT :045_1, :045_2, :045_3, :045_4, :045_5, :045_6, :045_7, :045_8, :045_9 FROM OneRowTable UNION ALL SELECT :046_1, :046_2, :046_3, :046_4, :046_5, :046_6, :046_7, :046_8, :046_9 FROM OneRowTable UNION ALL SELECT :047_1, :047_2, :047_3, :047_4, :047_5, :047_6, :047_7, :047_8, :047_9 FROM OneRowTable UNION ALL SELECT :048_1, :048_2, :048_3, :048_4, :048_5, :048_6, :048_7, :048_8, :048_9 FROM OneRowTable UNION ALL SELECT :049_1, :049_2, :049_3, :049_4, :049_5, :049_6, :049_7, :049_8, :049_9 FROM OneRowTable ) AS DT1 WHERE NOT ( DT1.p IS NULL AND DT1.cfYear IS NULL AND DT1.cfLocation IS NULL AND DT1.Delta IS NULL AND DT1.Design IS NULL AND DT1.SigmaLoc IS NULL AND DT1.Sigma IS NULL AND DT1.SampleSize IS NULL AND DT1.Intercept IS NULL ) ;Обратите внимание, что приведенный выше синтаксис режима запроса ANSI-92 (см. http://office.microsoft.co
qaru.site
Как вставить несколько строк без повторения части инструкции INSERT INTO dbo.Blah? [sql-server-2005] [tsql]
INSERT INTO dbo.MyTable (ID, Name) SELECT 123, 'Timmy' UNION ALL SELECT 124, 'Jonny' UNION ALL SELECT 125, 'Sally'Для SQL Server 2008 можно сделать это в одном предложении VALUES точно так же, как в заявлении в вашем вопросе (вам просто нужно добавить запятую, чтобы отделить каждое значение) ...
Если ваши данные уже находятся в вашей базе данных, вы можете:
INSERT INTO MyTable(ID, Name) SELECT ID, NAME FROM OtherTableЕсли вам нужно жестко закодировать данные, тогда SQL 2008 и более поздние версии позволят вам сделать следующее ...
INSERT INTO MyTable (Name, ID) VALUES ('First',1), ('Second',2), ('Third',3), ('Fourth',4), ('Fifth',5)Вы можете использовать объединение:
INSERT INTO dbo.MyTable (ID, Name) SELECT ID, Name FROM ( SELECT 123, 'Timmy' UNION ALL SELECT 124, 'Jonny' UNION ALL SELECT 125, 'Sally' ) AS X (ID, Name)Это выглядит нормально для SQL Server 2008. Для SS2005 и более ранних версий вам нужно повторить оператор VALUES.
INSERT INTO dbo.MyTable (ID, Name) VALUES (123, 'Timmy') VALUES (124, 'Jonny') VALUES (125, 'Sally')EDIT :: Мой плохой. Вы должны повторить «INSERT INTO» для каждой строки в SS2005.
INSERT INTO dbo.MyTable (ID, Name) VALUES (123, 'Timmy') INSERT INTO dbo.MyTable (ID, Name) VALUES (124, 'Jonny') INSERT INTO dbo.MyTable (ID, Name) VALUES (125, 'Sally') USE YourDB GO INSERT INTO MyTable (FirstCol, SecondCol) SELECT 'First' ,1 UNION ALL SELECT 'Second' ,2 UNION ALL SELECT 'Third' ,3 UNION ALL SELECT 'Fourth' ,4 UNION ALL SELECT 'Fifth' ,5 GOИЛИ ВЫ МОЖЕТЕ ИСПОЛЬЗОВАТЬ ДРУГОЙ ПУТЬ
INSERT INTO MyTable (FirstCol, SecondCol) VALUES ('First',1), ('Second',2), ('Third',3), ('Fourth',4), ('Fifth',5)Я использовал следующее:
INSERT INTO [TableName] (ID, Name) values (NEWID(), NEWID()) GO 10Он добавит десять строк с уникальными идентификаторами GUID для ID и Name.
Примечание: не заканчивайте последнюю строку (GO 10) на ';' потому что это вызовет ошибку: произошла фатальная ошибка сценария. Неправильный синтаксис встречался при разборе GO.
code-examples.net
обрабатывать несколько строк в триггере BEFORE INSERT при вставке нескольких строк через один SQL-запрос
Вы можете использовать левое соединение и рассчитывать на существующие записи для достижения только вставки записей, когда есть 3 или более записей, которые уже были сохранены, или вставка имеет как минимум три записи для вставки для идентификатора пользователя.
CREATE TABLE if not exists `product_46353892` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `user_id` int(11) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8; /* truncate table to allow rerun of example */ truncate product_46353892 ; /* example showing no inserts since there are only two records being inserted and no previous records for this user_id */ insert into product_46353892 ( id, user_id ) select `new`.* -- , new_count.count new_count -- , old.count old_count from ( select 1 id, 1 user_id union all select 2 id, 1 user_id ) `new` left join ( select user_id, count(*) count from ( select 1 id, 1 user_id union all select 2 id, 1 user_id ) new_group group by user_id ) new_count on `new`.user_id = new_count.user_id left join ( select user_id, count(*) count from product_46353892 product group by user_id ) old on new.user_id = old.user_id where ( new_count.count >= 3 or old.count >= 3 or ( new_count.count + old.count ) >= 3 ) ; /* this one works because there are three or more records in the insert */ insert into product_46353892 ( id, user_id ) select `new`.* from ( select 3 id, 1 user_id union all select 4 id, 1 user_id union all select 5 id, 1 user_id union all select 6 id, 1 user_id ) `new` left join ( select user_id, count(*) count from ( select 3 id, 1 user_id union all select 4 id, 1 user_id union all select 5 id, 1 user_id union all select 6 id, 1 user_id ) new_group group by user_id ) new_count on `new`.user_id = new_count.user_id left join ( select user_id, count(*) count from product_46353892 product group by user_id ) old on new.user_id = old.user_id where ( new_count.count >= 3 or old.count >= 3 or ( new_count.count + old.count ) >= 3 ) ; /* this now works since there are already the required number of initial records stored for this user_id */ insert into product_46353892 ( id, user_id ) select `new`.* -- , new_count.count new_count -- , old.count old_count from ( select 1 id, 1 user_id union all select 2 id, 1 user_id ) `new` left join ( select user_id, count(*) count from ( select 1 id, 1 user_id union all select 2 id, 1 user_id ) new_group group by user_id ) new_count on `new`.user_id = new_count.user_id left join ( select user_id, count(*) count from product_46353892 product group by user_id ) old on new.user_id = old.user_id where ( new_count.count >= 3 or old.count >= 3 or ( new_count.count + old.count ) >= 3 ) ; select * from product_46353892 ;Примеры результатов. Обратите внимание, что порядок идентификаторов, 1 и 2 был вставлен во вторую вставку после того, как было уже три или более записей для идентификатора пользователя в таблице.
id user_id 3 1 4 1 5 1 6 1 1 1 2 1sqlserver.bilee.com
insert - Вставка нескольких строк в SQL Server
Вы можете сделать это:
WITH Temp AS ( SELECT n FROM(VALUES(0), (1), (2), (3), (4), (5), (6), (7), (8), (9)) AS Temp(n) ), nums AS ( SELECT id = t1.n * 10 + t2.n + 1 FROM temp AS T1 CROSS JOIN temp AS t2 ) INSERT INTO ProductSerialNumber(serialnumber) SELECT 12345679000 + id AS Serialnumber -- You can insert into other columns too FROM nums;Демо-версия SQL Fiddle.
Обратите внимание: этот синтаксис FROM(VALUES(0), (1),..., (9)) AS Temp(n) является новым для SQL Server-2008, для старых версий вы можете использовать SELECT... UNION ALL SELECT... вместо этого.
Однако, если это возможно, вы можете изменить эту таблицу и сделать этот столбец SerialNumber IDENTITY(12345679000, 1) и он будет автоматически инкрементным.
Обновление 1
Для SQL Server 2005 попробуйте следующее:
WITH Temp AS ( SELECT 1 AS id UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 ), nums AS ( SELECT ROW_NUMBER() OVER(ORDER BY t1.id) AS id FROM temp t1, temp t2, temp t3, temp t4 ) INSERT INTO ProductSerialNumber(serialnumber) SELECT 12345679000 + id AS Serialnumber FROM nums WHERE id <= 100;Обновлена демо-версия скрипта SQL.
Обновление 2
* Как работает этот запрос? *
Во-первых, я определяю виртуальную таблицу с четырьмя значениями:
SELECT 1 AS id UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4Я определил его внутри выражения Common Table (CTE), чтобы повторно использовать его позже.
Затем в следующем CTE я использовал:
FROM temp t1, temp t2, temp t3, temp t4Это объединит таблицу temp четыре раза с ней, поэтому она даст вам: 4 4= 256 строк. Затем я использовал функцию ранжирования ROW_NUMBER() как произведение раунда для генерации порядкового номера числа от 1 до 265.
Последняя вещь - синтаксис INSERT INTO... SELECT... для выбора чисел <= 100 чисел, которые мы уже сгенерировали на предыдущем шаге, и вставки их в таблицу.
Надеюсь, это имеет смысл.
qaru.site
sql - .net sql INSERT несколько строк с addWithValue
имея этот код (который я знаю, что open/executenonquery/close conection in for loop абсолютно ужасен).
Public Shared Sub salvarDatos(ByRef conexion As SqlConnection, ByRef datos As List(Of DatoBdReloj)) For i = 0 To datos.Count - 1 Dim laQuery As String = "Insert INTO MARCADASRELOJ (COD_MAR_RELOJ,FECHA_RELOJ,HORA_RELOJ,MINUTOS_RELOJ,NRO_RELOJ,NRO_TARJ) " & _ "VALUES (@codMarReloj,@fechaReloj,@horaReloj,@minutosReloj,@nroReloj,@nroTarj)" Dim datoReloj As DatoBdReloj = datos.Item(i) Dim command As SqlCommand = New SqlCommand(laQuery, conexion) command.Parameters.AddWithValue("codMarReloj", datoReloj.tipoMarca) command.Parameters.AddWithValue("fechaReloj", datoReloj.fechaHoraMarcaDT) command.Parameters.AddWithValue("horaReloj", datoReloj.fechaHoraMarcaDT.Hour) command.Parameters.AddWithValue("minutosReloj", datoReloj.fechaHoraMarcaDT.Minute) command.Parameters.AddWithValue("nroReloj", datoReloj.idReloj) command.Parameters.AddWithValue("nroTarj", datoReloj.idUsuario) conexion.Open() command.ExecuteNonQuery() conexion.Close() Next End SubУ меня нет большого опыта работы с.net, но для того, что я читал с помощью метода AddWithValue, я могу предотвратить инъекции sql и, согласно "книге", это правильный способ сделать это (скажите, если я ошибаюсь).
используя предложение типа "Вставить в значения xxx (x1, x2, x3)" ("val", "val", "val"), (...), (...) "для вставки нескольких строк не является опцией для меня, потому что моя база данных работает с sql 2005.
Как я могу отнять этот ужасный open/execute/close внутри цикла for, чтобы вставить несколько строк независимо от того, был ли он sql 2005 или 2008, а также сохранить безопасность от SQL-инъекций с помощью метода AddWithValue?
Благодарю! :)
qaru.site