插入查询一次插入两个表 [英] insert query to insert into two table at once

查看:101
本文介绍了插入查询一次插入两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#,asp.net,VS 2005和sql server 2005

我正在使用以下插入查询将图像文件插入sql server2005.

I am using C#,asp.net,VS 2005 and sql server 2005

i am using the following insert query to insert image file into sql server 2005.

SqlCommand command = new SqlCommand("insert into MyImageTable(ImageName,ImageFile,MIMEType,ImageId,MYID) values (@img_name,@img_file,@img_type,@img_id,@my_ID)", connection);



任何人都可以帮助我如何通过修改上述查询将相同的图像文件插入到另一个表中.我想插入图像文件,并且已在sql server 2005中将数据类型指定为图像.

在此先感谢.
关于
karan



Can anybody help how i can insert the same image file into another table by modifying the above query. i want to insert the image file and i have given datatype as image in sql server 2005.

thanks in advance.
with regards
karan

推荐答案

使用新表名重复该命令.
INSERT语句不能插入到1个以上的表中.另一种选择是存储过程,并将存储过程插入2个表中.
Repeat the command and with new table name.
INSERT statement cannot insert to more then 1 table. The other option is to stored procedure and have stored procedure insert into 2 tables.


为什么要在两个位置存储相同的映像?这是很大的空间浪费,并导致两者可能失步.相反,为什么不创建一个仅包含图像和相关信息以及ID的图像表,并从两个表中再次引用该图像呢?
Why do you want to store the same image in two places? It is a big waste of space, and leads to the possiblity of the two getting out of step. Instead, why not create a table of images, with just the image and related info, and an ID, and refer back to that from both tables?


试试这个

让我解释一下,假设我有两个表"abc"和"xyz"

首先,我创建了以下存储过程.

创建PROCEDURE procemp
@Empcode数字,
@FName nvarchar(100),
@LName nvarchar(100),
@sex nvarchar(100),
@age数值
AS
开始
插入abc(Empcode,FName,LName)值(@ Empcode,@ FName,@LName)
插入xyz(Empcode,sex,age)值(@Empcode,@ sex,@ age)
END
GO

创建上述过程后,我通过提供以下参数来执行该过程:

执行procemp 1,''a'',``b'',''male'',28
Try this

let me explain suppose i have two tables ''abc'' and ''xyz''

First of all i have created the following storedprocedure.

Create PROCEDURE procemp
@Empcode numeric,
@FName nvarchar(100),
@LName nvarchar(100),
@sex nvarchar(100),
@age numeric
AS
BEGIN
Insert into abc(Empcode,FName, LName) values (@Empcode,@FName, @LName)
Insert into xyz(Empcode,sex,age) values (@Empcode, @sex,@age)
END
GO

after creating the above procedure i executed this procedure by providing parameters as under

execute procemp 1,''a'', ''b'',''male'',28


这篇关于插入查询一次插入两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆