behradnasehi
سه شنبه 22 دی 1394, 10:17 صبح
164
فرض کنیم دو جدول بشکل زیر را داریم:
جدول Factor
CREATE TABLE Factor(
[id] [BIGINT] IDENTITY(1,1) NOT NULL,
[salmali] [BIGINT] NOT NULL,
[shomare] [BIGINT] NOT NULL,
[tarikhe] [NVARCHAR](10) NOT NULL
)
جدول جزییات Factor_Copy
CREATE TABLE Factor_Copy(
[id] [BIGINT] IDENTITY(1,1) NOT NULL,
[salmali] [BIGINT] NOT NULL,
[shomare] [BIGINT] NOT NULL,
[tarikhe] [NVARCHAR](10) NOT NULL
)
اگر بخواهیم رکوردهای جدول Factor را در Factor_Copy درج کنیم کدی بشکل زیر مینویسیم
insert into Factor_Copy ( salmali, shomare, tarikhe )
select salmali,shomare,tarikhe
from Factor
اگر بخواهیم کدهای identity ایجاد شده در جدول Factor_Copy و کد مرتبط با جدول Factor را بدست آوریم باید بشکل زیر عمل کنیم:
MERGE Factor_Copy as targetUSING (
SELECT *
FROM Factor
) AS source
ON (1=0) -- make sure the result is False
WHEN NOT MATCHED BY TARGET THEN
insert ( salmali, shomare, tarikhe) VALUES (source.salmali,source.shomare,source.tarikhe)
OUTPUT $action,source.id, INSERTED.* INTO #tmp;
فرض کنیم دو جدول بشکل زیر را داریم:
جدول Factor
CREATE TABLE Factor(
[id] [BIGINT] IDENTITY(1,1) NOT NULL,
[salmali] [BIGINT] NOT NULL,
[shomare] [BIGINT] NOT NULL,
[tarikhe] [NVARCHAR](10) NOT NULL
)
جدول جزییات Factor_Copy
CREATE TABLE Factor_Copy(
[id] [BIGINT] IDENTITY(1,1) NOT NULL,
[salmali] [BIGINT] NOT NULL,
[shomare] [BIGINT] NOT NULL,
[tarikhe] [NVARCHAR](10) NOT NULL
)
اگر بخواهیم رکوردهای جدول Factor را در Factor_Copy درج کنیم کدی بشکل زیر مینویسیم
insert into Factor_Copy ( salmali, shomare, tarikhe )
select salmali,shomare,tarikhe
from Factor
اگر بخواهیم کدهای identity ایجاد شده در جدول Factor_Copy و کد مرتبط با جدول Factor را بدست آوریم باید بشکل زیر عمل کنیم:
MERGE Factor_Copy as targetUSING (
SELECT *
FROM Factor
) AS source
ON (1=0) -- make sure the result is False
WHEN NOT MATCHED BY TARGET THEN
insert ( salmali, shomare, tarikhe) VALUES (source.salmali,source.shomare,source.tarikhe)
OUTPUT $action,source.id, INSERTED.* INTO #tmp;