SQL Server : insert into another table values if requirement met in other table
up vote
0
down vote
favorite
DECLARE @AccountID VARCHAR(10)
SELECT @AccountID = AccountID
FROM MuOnlineSlow.dbo.T_VIPList
WHERE Type = 1
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE ([AccountID], [name_full], [name], [history], [hex], [cat], [itemid], [level], [luck], [skill], [excellent], [ancient], [time], [has_exe_1], [has_exe_2], [has_exe_4], [has_exe_8], [has_exe_16], [has_exe_32], [socket_exe_6], [socket_exe_7], [socket_exe_8], [server])
VALUES (@AccountID,
'<span style="color:;font-family: tahoma;font-size: 12px;">Bundle of Jewel of Soul </span>',
'Bundle of Jewel of Soul',
'VIP REWARD',
'1F0000000000000000C000FFFFFFFFFF0001D560FFFFFFFFFFFFFFFFFFFFFFFF',
12, 31, 0, 0, 0, 0, 0,
1542466494, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'DEFAULT')
Problem is that query inserts only last information found in T_VIPList table into WEB_WAREHOUSE table, but I have 6 rows with different AccountID. Why doesn't it enter the new information in the WEB_WAREHOUSE with all AccountID?
Any ideas?
add a comment |
up vote
0
down vote
favorite
DECLARE @AccountID VARCHAR(10)
SELECT @AccountID = AccountID
FROM MuOnlineSlow.dbo.T_VIPList
WHERE Type = 1
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE ([AccountID], [name_full], [name], [history], [hex], [cat], [itemid], [level], [luck], [skill], [excellent], [ancient], [time], [has_exe_1], [has_exe_2], [has_exe_4], [has_exe_8], [has_exe_16], [has_exe_32], [socket_exe_6], [socket_exe_7], [socket_exe_8], [server])
VALUES (@AccountID,
'<span style="color:;font-family: tahoma;font-size: 12px;">Bundle of Jewel of Soul </span>',
'Bundle of Jewel of Soul',
'VIP REWARD',
'1F0000000000000000C000FFFFFFFFFF0001D560FFFFFFFFFFFFFFFFFFFFFFFF',
12, 31, 0, 0, 0, 0, 0,
1542466494, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'DEFAULT')
Problem is that query inserts only last information found in T_VIPList table into WEB_WAREHOUSE table, but I have 6 rows with different AccountID. Why doesn't it enter the new information in the WEB_WAREHOUSE with all AccountID?
Any ideas?
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
DECLARE @AccountID VARCHAR(10)
SELECT @AccountID = AccountID
FROM MuOnlineSlow.dbo.T_VIPList
WHERE Type = 1
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE ([AccountID], [name_full], [name], [history], [hex], [cat], [itemid], [level], [luck], [skill], [excellent], [ancient], [time], [has_exe_1], [has_exe_2], [has_exe_4], [has_exe_8], [has_exe_16], [has_exe_32], [socket_exe_6], [socket_exe_7], [socket_exe_8], [server])
VALUES (@AccountID,
'<span style="color:;font-family: tahoma;font-size: 12px;">Bundle of Jewel of Soul </span>',
'Bundle of Jewel of Soul',
'VIP REWARD',
'1F0000000000000000C000FFFFFFFFFF0001D560FFFFFFFFFFFFFFFFFFFFFFFF',
12, 31, 0, 0, 0, 0, 0,
1542466494, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'DEFAULT')
Problem is that query inserts only last information found in T_VIPList table into WEB_WAREHOUSE table, but I have 6 rows with different AccountID. Why doesn't it enter the new information in the WEB_WAREHOUSE with all AccountID?
Any ideas?
DECLARE @AccountID VARCHAR(10)
SELECT @AccountID = AccountID
FROM MuOnlineSlow.dbo.T_VIPList
WHERE Type = 1
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE ([AccountID], [name_full], [name], [history], [hex], [cat], [itemid], [level], [luck], [skill], [excellent], [ancient], [time], [has_exe_1], [has_exe_2], [has_exe_4], [has_exe_8], [has_exe_16], [has_exe_32], [socket_exe_6], [socket_exe_7], [socket_exe_8], [server])
VALUES (@AccountID,
'<span style="color:;font-family: tahoma;font-size: 12px;">Bundle of Jewel of Soul </span>',
'Bundle of Jewel of Soul',
'VIP REWARD',
'1F0000000000000000C000FFFFFFFFFF0001D560FFFFFFFFFFFFFFFFFFFFFFFF',
12, 31, 0, 0, 0, 0, 0,
1542466494, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'DEFAULT')
Problem is that query inserts only last information found in T_VIPList table into WEB_WAREHOUSE table, but I have 6 rows with different AccountID. Why doesn't it enter the new information in the WEB_WAREHOUSE with all AccountID?
Any ideas?
edited Nov 21 at 21:23
marc_s
568k12810991249
568k12810991249
asked Nov 21 at 20:58
wiraly
31
31
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
I think you want something closer to this, which will SELECT all of the AccountIDs from your VIPList with Type 1:
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE
([AccountID], ..., [server]))
SELECT vip.AccountID, ..., 'DEFAULT'
FROM MuOnlineSlow.dbo.T_VIPList vip
WHERE vip.Type = 1;
And, assuming AccountID is your primary key, you could make the insert safer by checking for existence:
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE
([AccountID], ..., [server]))
SELECT vip.AccountID, ..., 'DEFAULT'
FROM MuOnlineSlow.dbo.T_VIPList vip
WHERE vip.Type = 1
AND NOT EXISTS(SELECT 1
FROM MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE w
WHERE w.AccountID = vip.AccountID);
Well my idea is to insert manually typed Values with replaced AccountID from T_VIPList table. Everything works, but like i said, it inserts only last rows AccountID.
– wiraly
Nov 21 at 21:16
@wiraly The fundamental problem with your approach is that@AccountIDis a variable that can only hold a single value. You need to use aSELECTstatement to get all of the different AccountIDs in a singleINSERT.
– Joe Stefanelli
Nov 21 at 21:19
1
Good Job. Thank you for your answer. This worker like i wanted - INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE ([AccountID], ..., [server])) SELECT vip.AccountID, ..., 'DEFAULT' FROM MuOnlineSlow.dbo.T_VIPList vip WHERE vip.Type = 1;
– wiraly
Nov 21 at 21:35
add a comment |
up vote
0
down vote
@AccountID is a variable that can only hold one value, not a list/array/vector. So if you wonder why your SELECT doesn't throw an error when the FROM with WHERE returns 6 rows and therefore 6 values are to be assigned to a variable: because this is one of the nondeterministic behaviours of SQL. A random value "wins" and will be assigned from the 6 returned. So you need to combine INSERT with SELECT without variables to get all rows (= a table) as other answer(s) show(s).
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I think you want something closer to this, which will SELECT all of the AccountIDs from your VIPList with Type 1:
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE
([AccountID], ..., [server]))
SELECT vip.AccountID, ..., 'DEFAULT'
FROM MuOnlineSlow.dbo.T_VIPList vip
WHERE vip.Type = 1;
And, assuming AccountID is your primary key, you could make the insert safer by checking for existence:
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE
([AccountID], ..., [server]))
SELECT vip.AccountID, ..., 'DEFAULT'
FROM MuOnlineSlow.dbo.T_VIPList vip
WHERE vip.Type = 1
AND NOT EXISTS(SELECT 1
FROM MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE w
WHERE w.AccountID = vip.AccountID);
Well my idea is to insert manually typed Values with replaced AccountID from T_VIPList table. Everything works, but like i said, it inserts only last rows AccountID.
– wiraly
Nov 21 at 21:16
@wiraly The fundamental problem with your approach is that@AccountIDis a variable that can only hold a single value. You need to use aSELECTstatement to get all of the different AccountIDs in a singleINSERT.
– Joe Stefanelli
Nov 21 at 21:19
1
Good Job. Thank you for your answer. This worker like i wanted - INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE ([AccountID], ..., [server])) SELECT vip.AccountID, ..., 'DEFAULT' FROM MuOnlineSlow.dbo.T_VIPList vip WHERE vip.Type = 1;
– wiraly
Nov 21 at 21:35
add a comment |
up vote
0
down vote
accepted
I think you want something closer to this, which will SELECT all of the AccountIDs from your VIPList with Type 1:
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE
([AccountID], ..., [server]))
SELECT vip.AccountID, ..., 'DEFAULT'
FROM MuOnlineSlow.dbo.T_VIPList vip
WHERE vip.Type = 1;
And, assuming AccountID is your primary key, you could make the insert safer by checking for existence:
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE
([AccountID], ..., [server]))
SELECT vip.AccountID, ..., 'DEFAULT'
FROM MuOnlineSlow.dbo.T_VIPList vip
WHERE vip.Type = 1
AND NOT EXISTS(SELECT 1
FROM MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE w
WHERE w.AccountID = vip.AccountID);
Well my idea is to insert manually typed Values with replaced AccountID from T_VIPList table. Everything works, but like i said, it inserts only last rows AccountID.
– wiraly
Nov 21 at 21:16
@wiraly The fundamental problem with your approach is that@AccountIDis a variable that can only hold a single value. You need to use aSELECTstatement to get all of the different AccountIDs in a singleINSERT.
– Joe Stefanelli
Nov 21 at 21:19
1
Good Job. Thank you for your answer. This worker like i wanted - INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE ([AccountID], ..., [server])) SELECT vip.AccountID, ..., 'DEFAULT' FROM MuOnlineSlow.dbo.T_VIPList vip WHERE vip.Type = 1;
– wiraly
Nov 21 at 21:35
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I think you want something closer to this, which will SELECT all of the AccountIDs from your VIPList with Type 1:
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE
([AccountID], ..., [server]))
SELECT vip.AccountID, ..., 'DEFAULT'
FROM MuOnlineSlow.dbo.T_VIPList vip
WHERE vip.Type = 1;
And, assuming AccountID is your primary key, you could make the insert safer by checking for existence:
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE
([AccountID], ..., [server]))
SELECT vip.AccountID, ..., 'DEFAULT'
FROM MuOnlineSlow.dbo.T_VIPList vip
WHERE vip.Type = 1
AND NOT EXISTS(SELECT 1
FROM MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE w
WHERE w.AccountID = vip.AccountID);
I think you want something closer to this, which will SELECT all of the AccountIDs from your VIPList with Type 1:
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE
([AccountID], ..., [server]))
SELECT vip.AccountID, ..., 'DEFAULT'
FROM MuOnlineSlow.dbo.T_VIPList vip
WHERE vip.Type = 1;
And, assuming AccountID is your primary key, you could make the insert safer by checking for existence:
INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE
([AccountID], ..., [server]))
SELECT vip.AccountID, ..., 'DEFAULT'
FROM MuOnlineSlow.dbo.T_VIPList vip
WHERE vip.Type = 1
AND NOT EXISTS(SELECT 1
FROM MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE w
WHERE w.AccountID = vip.AccountID);
edited Nov 21 at 21:10
answered Nov 21 at 21:03
Joe Stefanelli
109k13190204
109k13190204
Well my idea is to insert manually typed Values with replaced AccountID from T_VIPList table. Everything works, but like i said, it inserts only last rows AccountID.
– wiraly
Nov 21 at 21:16
@wiraly The fundamental problem with your approach is that@AccountIDis a variable that can only hold a single value. You need to use aSELECTstatement to get all of the different AccountIDs in a singleINSERT.
– Joe Stefanelli
Nov 21 at 21:19
1
Good Job. Thank you for your answer. This worker like i wanted - INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE ([AccountID], ..., [server])) SELECT vip.AccountID, ..., 'DEFAULT' FROM MuOnlineSlow.dbo.T_VIPList vip WHERE vip.Type = 1;
– wiraly
Nov 21 at 21:35
add a comment |
Well my idea is to insert manually typed Values with replaced AccountID from T_VIPList table. Everything works, but like i said, it inserts only last rows AccountID.
– wiraly
Nov 21 at 21:16
@wiraly The fundamental problem with your approach is that@AccountIDis a variable that can only hold a single value. You need to use aSELECTstatement to get all of the different AccountIDs in a singleINSERT.
– Joe Stefanelli
Nov 21 at 21:19
1
Good Job. Thank you for your answer. This worker like i wanted - INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE ([AccountID], ..., [server])) SELECT vip.AccountID, ..., 'DEFAULT' FROM MuOnlineSlow.dbo.T_VIPList vip WHERE vip.Type = 1;
– wiraly
Nov 21 at 21:35
Well my idea is to insert manually typed Values with replaced AccountID from T_VIPList table. Everything works, but like i said, it inserts only last rows AccountID.
– wiraly
Nov 21 at 21:16
Well my idea is to insert manually typed Values with replaced AccountID from T_VIPList table. Everything works, but like i said, it inserts only last rows AccountID.
– wiraly
Nov 21 at 21:16
@wiraly The fundamental problem with your approach is that
@AccountID is a variable that can only hold a single value. You need to use a SELECT statement to get all of the different AccountIDs in a single INSERT.– Joe Stefanelli
Nov 21 at 21:19
@wiraly The fundamental problem with your approach is that
@AccountID is a variable that can only hold a single value. You need to use a SELECT statement to get all of the different AccountIDs in a single INSERT.– Joe Stefanelli
Nov 21 at 21:19
1
1
Good Job. Thank you for your answer. This worker like i wanted - INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE ([AccountID], ..., [server])) SELECT vip.AccountID, ..., 'DEFAULT' FROM MuOnlineSlow.dbo.T_VIPList vip WHERE vip.Type = 1;
– wiraly
Nov 21 at 21:35
Good Job. Thank you for your answer. This worker like i wanted - INSERT INTO MUONLINEWEBS.dbo.MUONLINEWEBS_WEB_WAREHOUSE ([AccountID], ..., [server])) SELECT vip.AccountID, ..., 'DEFAULT' FROM MuOnlineSlow.dbo.T_VIPList vip WHERE vip.Type = 1;
– wiraly
Nov 21 at 21:35
add a comment |
up vote
0
down vote
@AccountID is a variable that can only hold one value, not a list/array/vector. So if you wonder why your SELECT doesn't throw an error when the FROM with WHERE returns 6 rows and therefore 6 values are to be assigned to a variable: because this is one of the nondeterministic behaviours of SQL. A random value "wins" and will be assigned from the 6 returned. So you need to combine INSERT with SELECT without variables to get all rows (= a table) as other answer(s) show(s).
add a comment |
up vote
0
down vote
@AccountID is a variable that can only hold one value, not a list/array/vector. So if you wonder why your SELECT doesn't throw an error when the FROM with WHERE returns 6 rows and therefore 6 values are to be assigned to a variable: because this is one of the nondeterministic behaviours of SQL. A random value "wins" and will be assigned from the 6 returned. So you need to combine INSERT with SELECT without variables to get all rows (= a table) as other answer(s) show(s).
add a comment |
up vote
0
down vote
up vote
0
down vote
@AccountID is a variable that can only hold one value, not a list/array/vector. So if you wonder why your SELECT doesn't throw an error when the FROM with WHERE returns 6 rows and therefore 6 values are to be assigned to a variable: because this is one of the nondeterministic behaviours of SQL. A random value "wins" and will be assigned from the 6 returned. So you need to combine INSERT with SELECT without variables to get all rows (= a table) as other answer(s) show(s).
@AccountID is a variable that can only hold one value, not a list/array/vector. So if you wonder why your SELECT doesn't throw an error when the FROM with WHERE returns 6 rows and therefore 6 values are to be assigned to a variable: because this is one of the nondeterministic behaviours of SQL. A random value "wins" and will be assigned from the 6 returned. So you need to combine INSERT with SELECT without variables to get all rows (= a table) as other answer(s) show(s).
answered Nov 21 at 21:20
Dávid Laczkó
36118
36118
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53420390%2fsql-server-insert-into-another-table-values-if-requirement-met-in-other-table%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown