想要将数据从一个表插入到另一个数据库中的另一个表 [英] Want to insert Data from one table to another from different database

查看:72
本文介绍了想要将数据从一个表插入到另一个数据库中的另一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨......伙计们

我的问题是跟随

插入TargetDatabase.dbo.tblContact

从SourceDatabase.dbo.tblContact中选择*


如上图所示.我想将同一表的数据插入同一表,但数据库不同

我尝试了以下

Hi.... Guys

my problem is following

insert into TargetDatabase.dbo.tblContact

Select * from SourceDatabase.dbo.tblContact


As shown above. i want to insert data of same table into Same table but database is different

I tried follwing

Create Procedure Demo
@SourceDatabase as  nvarchar(100),
@TargetDatabase as  nvarchar(100)
as 
exec ( 'insert into' +@TargetDatabase+'.dbo.tblContact')
exec('select * from ' +@SourceDatabase+'.dbo.tblContact')


在此代码中,选择查询"正常工作

但是在插入时会引发错误'' tbl Contact附近的语法不正确.''

欢迎任何帮助.

请帮忙.


In this code Select Query is working Fine

but while inserting it is throwing error ''Incorrect syntax near tbl Contact.''

Any help is welcome.

Please help.

推荐答案

您需要使其成为单个exec语句:
exec(``插入''+@TargetDatabase+''.dbo.tblContact select * from``+ @ SourceDatabase +''.dbo.tblContact'')
You need to make it a single exec statement:
exec ( ''insert into'' +@TargetDatabase+''.dbo.tblContact select * from '' +@SourceDatabase+''.dbo.tblContact'')


更改"exec"如:
Change the "exec" as:
exec ( 'insert into' +@TargetDatabase+'.dbo.tblContact ' + 
       'select * from ' +@SourceDatabase+'.dbo.tblContact')


exec ( ''insert into'' +@TargetDatabase+''.dbo.tblContact'')
您尚未在此表中包含任何列.这样就得到了错误.

您的查询应该是
''insert into'' + @TargetDatabase+''.dbo.tblContact'' (columna,columnb...) values (value1,value2)
exec ( ''insert into'' +@TargetDatabase+''.dbo.tblContact'')
You have not included any columns of the table here. Thus you get the error.

Your query ought to be
''insert into'' + @TargetDatabase+''.dbo.tblContact'' (columna,columnb...) values (value1,value2)


这篇关于想要将数据从一个表插入到另一个数据库中的另一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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