Sql server 使用和用户数据将记录从一个表插入到另一个表。.SQL server

Sql server 使用和用户数据将记录从一个表插入到另一个表。.SQL server,sql-server,Sql Server,我有以下示例t-sql语句: INSERT INTO [Northwind].[dbo].[Categories]([CategoryName], [Description],Picture) SELECT TOP 10 productname, quantityperunit FROM [Northwind].[dbo].Products 我所做的非常简单,但我想将图片添加到我的表中,我想从我自己的数据中添加它。我知道这是不对的,但我认为这比我能解释的更好: INSERT INTO [

我有以下示例t-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!!'
因此,我希望前两个字段[CategoryName]、[Description]来自Products表中的记录。我希望最后一个字段[图片]填充我自己的数据

这只是我想要实现的一个例子。我希望前两个字段之外的数据相同。我不会在生产中使用Northwind。我只是在找语法。如果您的问题只是:如何向插入列表中添加值,请多谢?您可以查看以下示例:

DECLARE @tblSource TABLE(ID INT, SomeData VARCHAR(100));
INSERT INTO @tblSource VALUES(1,'Data 1'),(2, 'Data 2');
--目标列可以有其他名称,但类型必须兼容:

DECLARE @tblTarget TABLE(ID INT,SomeData VARCHAR(100),OneMore VARCHAR(100));
--插入两行@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
如果您的问题只是:如何向插入列表添加值?您可以查看以下示例:

DECLARE @tblSource TABLE(ID INT, SomeData VARCHAR(100));
INSERT INTO @tblSource VALUES(1,'Data 1'),(2, 'Data 2');
--目标列可以有其他名称,但类型必须兼容:

DECLARE @tblTarget TABLE(ID INT,SomeData VARCHAR(100),OneMore VARCHAR(100));
--插入两行@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
你离这里很近。
我不知道northwind表,但是如果列picture是一个varchar字段,那么这将用一个固定字符串填充表产品的前两列和第三列。 这就是你的意思吗

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子句中添加静态数据。

我不知道northwind表,但是如果列picture是一个varchar字段,那么这将用一个固定字符串填充表产品的前两列和第三列。 这就是你的意思吗

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子句中。

那么“myimagedata”从何而来?如何为这10条记录中的每一条填充正确的图像?你没有抓住要点。我不会在生产中使用Northwind。我的问题不是如何填充图像字段。我需要关于如何从表中的记录和我自己的数据填充字段的语法。“myimagedata”从何而来?如何为这10条记录中的每一条填充正确的图像?你没有抓住要点。我不会在生产中使用Northwind。我的问题不是如何填充图像字段。我想要关于如何从表中的记录和我自己的数据填充字段的语法。完美!!这正是我想要的。完美!!这正是我想要的。