从一个表插入到另一个表(不同的数据库)

时间:2016-08-29 07:32:53

标签: sql sql-server

Auth2.dbo.Accounts: account_id,login_name,密码,其他列...

Auth.dbo.Account: account_id,帐户,密码,其他栏目......

我想在" Auth.dbo.Account "中插入帐户(account_id,帐户,密码)。到" Auth2.dbo.Accounts " (account_id,login_name,password)并给出我想要的其他列值。

我希望这可行,但是一旦我在SQL Management studio中写道,我在"附近出现了错误语法错误。从Auth.dbo.account"

中选择account_id
INSERT [dbo].[Accounts] ([account_id], [login_name], [password],
       [referral_id], [referral_code], [pcbang], [block],
       [withdraw_remain_time], [age], [auth_ok],
       [last_login_server_idx], [event_code], [server_list_mask], 
       [result], [ip], [game_code], [gamecode], [login_event],
       [email], [security_a_1], [security_a_2], [security_a_3],
       [security_a_4], [security_q_1], [security_q_2], [security_q_3],
       [security_q_4], [votepoints], [cash], [country]) 
VALUES (select account_id from auth.dbo.Account, N'Imad', 
               N'3cfbbd2ae3c3e416c6d00a5a12ee60e8', NULL, NULL, 
               0, 0, NULL, -1997, 1, NULL, NULL, NULL, NULL, 
               N'::1', NULL, NULL, NULL, N'imad.lekal@outlook.com',
               NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 
               0, 189, NULL)

3 个答案:

答案 0 :(得分:4)

尝试使用简单的INSERT/SELECT。在这种情况下,account_id将来自auth.dbo.Account,所有其他值将是常量。

INSERT Auth2.[dbo].[Accounts] ([account_id], [login_name], [password], [referral_id], [referral_code], [pcbang], [block], [withdraw_remain_time], [age], [auth_ok], [last_login_server_idx], [event_code], [server_list_mask], [result], [ip], [game_code], [gamecode], [login_event], [email], [security_a_1], [security_a_2], [security_a_3], [security_a_4], [security_q_1], [security_q_2], [security_q_3], [security_q_4], [votepoints], [cash], [country]) 
select account_id , N'Imad', N'3cfbbd2ae3c3e416c6d00a5a12ee60e8', NULL, NULL, 0, 0, NULL, -1997, 1, NULL, NULL, NULL, NULL, N'::1', NULL, NULL, NULL, N'imad.lekal@outlook.com', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 189, NULL from auth.dbo.Account;

答案 1 :(得分:2)

删除VALUES (

当您INSERT使用SELECT时,您不应使用VALUES,因为这些值来自SELECT

同样在表名前加上数据库名称,假设AuthAuth2是2个不同的数据库。

最后,您的FROM已关闭。

以下应该工作。

INSERT Auth2.dbo.Accounts 
(
[account_id]
, [login_name]
, [password]
, [referral_id]
, [referral_code]
, [pcbang]
, [block]
, [withdraw_remain_time]
, [age]
, [auth_ok]
, [last_login_server_idx]
, [event_code]
, [server_list_mask]
, [result]
, [ip]
, [game_code]
, [gamecode]
, [login_event]
, [email]
, [security_a_1]
, [security_a_2]
, [security_a_3]
, [security_a_4]
, [security_q_1]
, [security_q_2]
, [security_q_3]
, [security_q_4]
, [votepoints]
, [cash]
, [country]
) 
SELECT 
account_id 
, N'Imad'
, N'3cfbbd2ae3c3e416c6d00a5a12ee60e8'
, NULL
, NULL
, 0
, 0
, NULL
, -1997
, 1
, NULL
, NULL
, NULL
, NULL
, N'::1'
, NULL
, NULL
, NULL
, N'imad.lekal@outlook.com'
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, NULL
, 0
, 189
, NULL
FROM auth.dbo.Account

专业提示:正确格式化您的SQL,有助于调试:)。

答案 2 :(得分:1)

嗨,我认为这将首先帮助您创建数据库链接

CREATE DATABASE LINK your_db_link_name
 CONNECT TO Schema_Name
 IDENTIFIED BY <PWD>
 USING 'Your Database Name';

接下来你需要进行查询

INSERT [Accounts] ([account_id], [login_name], [password], [referral_id]
      , [referral_code], [pcbang], [block], [withdraw_remain_time], [age], [
      auth_ok], [last_login_server_idx], [event_code], [server_list_mask], [
      result], [ip], [game_code], [gamecode], [login_event], [email], [
      security_a_1], [security_a_2], [security_a_3], [security_a_4], [
      security_q_1], [security_q_2], [security_q_3], [security_q_4], [
      votepoints], [cash], [country])
      VALUES (
      select ac.account_id
      from Your_DB_Link_Name@Account ac, N'Imad', N'3cfbbd2ae3c3e416c6d00a5a12ee60e8',
      NULL, NULL, 0, 0, NULL, -1997, 1, NULL, NULL, NULL, NULL, N'::1', NULL,
      NULL, NULL, N'imad.lekal@outlook.com', NULL, NULL, NULL, NULL, NULL,
      NULL, NULL, NULL, 0, 189, NULL)

我认为如果您有任何疑问,这将对您有所帮助