Da black ninja Da black ninja - 3 months ago 6
SQL Question

Insert from one table to another (different databases)

Auth2.dbo.Accounts : account_id, login_name , password , other columns...

Auth.dbo.Account : account_id , account , password , other columns...

I want to insert accounts(account_id,account,password) from "Auth.dbo.Account" to "Auth2.dbo.Accounts" (account_id,login_name , password) and giving other columns values that I want.

I was hoping that this could work , but once I wrote in SQL Management studio , I got an error syntax error near the " select account_id from Auth.dbo.account"

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)

Answer

Try with simple INSERT/SELECT. In this case account_id will be from auth.dbo.Account and all the other values will be constants.

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;
Comments