2016-10-03 80 views
1

我有下面的示例T-SQL語句:插入記錄從一個表到另一個和用戶data..SQL服務器

INSERT INTO [Northwind].[dbo].[Categories]([CategoryName], [Description],Picture) 
SELECT TOP 10 productname, quantityperunit FROM [Northwind].[dbo].Products 

非常簡單的我在做什麼,但我想添加圖片放入我的表格中,我想從我自己的數據中添加它。我知道這是不正確的,但我認爲它具有更好的比我可以解釋我想要做的事:

INSERT INTO [Northwind].[dbo].[Categories]([CategoryName], [Description],Picture) 
(SELECT TOP 10 productname, quantityperunit FROM [Northwind].[dbo].Products),'this is dummy data not real!!' 

所以,第一個2場[類別名稱],[說明]我要來來自Products表中的記錄。我想用我自己的數據填充最後一個字段[圖片]。

這只是我想達到的一個樣本。我希望數據在前兩個字段之外是相同的。我不會在生產中使用Northwind。我只在尋找語法。謝謝

+2

'myimagedata'從哪裏來?它應該如何爲這10個記錄中的每一個填充正確的圖像? – GuidoG

+0

你錯過了這一點。我不會在生產中使用Northwind。我的問題不是如何填充圖像字段。我想要關於如何從表中的記錄和我自己的數據填充字段的語法。 – BoundForGlory

回答

3

如果你的問題只是:如何將值添加到插入列表?你可以看看下面這個例子:

DECLARE @tblSource TABLE(ID INT, SomeData VARCHAR(100)); 
INSERT INTO @tblSource VALUES(1,'Data 1'),(2, 'Data 2'); 

目標--The列可以有其他名稱,但類型必須是兼容的:

DECLARE @tblTarget TABLE(ID INT,SomeData VARCHAR(100),OneMore VARCHAR(100)); 

--insert @tblSource兩排用一個額外的值

INSERT INTO @tblTarget(ID,SomeData,OneMore) 
SELECT ID,SomeData,'Just add a static value into the list' 
FROM @tblSource; 

- 檢查結果

SELECT * FROM @tblTarget 

結果

ID SomeData OneMore 
1 Data 1  Just add a static value into the list 
2 Data 2  Just add a static value into the list 
+0

完美!這正是我想要的。 – BoundForGlory

1

你在哪裏非常接近。
我不知道northwind表,但如果列圖片將是一個varchar字段比這將填補表產品的第2列,第3個字段與一個固定的字符串。 這是你的意思嗎?

INSERT INTO [Northwind].[dbo].[Categories] 
    ([CategoryName], [Description], Picture) 
SELECT TOP 10 productname, quantityperunit, 'this is dummy data not real!!' 
FROM [Northwind].[dbo].Products) 

你可以從@Shnugo的回答看,這回答關鍵是要靜態數據添加到您的SELECT子句中查詢。

+0

謝謝。這將工作 – BoundForGlory

相關問題