SELECT and INSERT in SQL Server
up vote
0
down vote
favorite
Can you help me please? I wrote this code and I get an error
Incorrect syntax near the keyword SELECT
Here is my code
CREATE PROCEDURE dbo.spSetCommand
@Client_id INT,
@BcNumber INT,
@ArticleId INT,
@EntryNumber DECIMAL
AS
DECLARE @CommandId INT
BEGIN
INSERT INTO commands(client_id, bc_number, date_command)
VALUES (@Client_id, @BcNumber, GETDATE());
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
INSERT INTO entries(command_id, article_id, entry_number)
VALUES (@CommandId, @ArticleId, @EntryNumber);
END
Can someone help find the issue?
sql-server
add a comment |
up vote
0
down vote
favorite
Can you help me please? I wrote this code and I get an error
Incorrect syntax near the keyword SELECT
Here is my code
CREATE PROCEDURE dbo.spSetCommand
@Client_id INT,
@BcNumber INT,
@ArticleId INT,
@EntryNumber DECIMAL
AS
DECLARE @CommandId INT
BEGIN
INSERT INTO commands(client_id, bc_number, date_command)
VALUES (@Client_id, @BcNumber, GETDATE());
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
INSERT INTO entries(command_id, article_id, entry_number)
VALUES (@CommandId, @ArticleId, @EntryNumber);
END
Can someone help find the issue?
sql-server
I've never seen a DECLARE before BEGIN. Not sure if that might be your issue
– Brian White
Nov 9 at 6:44
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Can you help me please? I wrote this code and I get an error
Incorrect syntax near the keyword SELECT
Here is my code
CREATE PROCEDURE dbo.spSetCommand
@Client_id INT,
@BcNumber INT,
@ArticleId INT,
@EntryNumber DECIMAL
AS
DECLARE @CommandId INT
BEGIN
INSERT INTO commands(client_id, bc_number, date_command)
VALUES (@Client_id, @BcNumber, GETDATE());
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
INSERT INTO entries(command_id, article_id, entry_number)
VALUES (@CommandId, @ArticleId, @EntryNumber);
END
Can someone help find the issue?
sql-server
Can you help me please? I wrote this code and I get an error
Incorrect syntax near the keyword SELECT
Here is my code
CREATE PROCEDURE dbo.spSetCommand
@Client_id INT,
@BcNumber INT,
@ArticleId INT,
@EntryNumber DECIMAL
AS
DECLARE @CommandId INT
BEGIN
INSERT INTO commands(client_id, bc_number, date_command)
VALUES (@Client_id, @BcNumber, GETDATE());
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
INSERT INTO entries(command_id, article_id, entry_number)
VALUES (@CommandId, @ArticleId, @EntryNumber);
END
Can someone help find the issue?
sql-server
sql-server
edited Nov 9 at 5:17
marc_s
565k12610921245
565k12610921245
asked Nov 9 at 0:04
Armand Mamitiana Rakotoarisoa
154
154
I've never seen a DECLARE before BEGIN. Not sure if that might be your issue
– Brian White
Nov 9 at 6:44
add a comment |
I've never seen a DECLARE before BEGIN. Not sure if that might be your issue
– Brian White
Nov 9 at 6:44
I've never seen a DECLARE before BEGIN. Not sure if that might be your issue
– Brian White
Nov 9 at 6:44
I've never seen a DECLARE before BEGIN. Not sure if that might be your issue
– Brian White
Nov 9 at 6:44
add a comment |
3 Answers
3
active
oldest
votes
up vote
4
down vote
accepted
-- need to have brackets
set @CommandId = (SELECT command_id FROM commands WHERE bc_number = @BcNumber);
if there are multiple results from the query, this will fail, an error will come.
you can also use this:
SELECT @CommandID = command_id
FROM commands
WHERE bc_number = @BcNumber
In case of multiple results, this will give you the last value (which may/may not be your logical thing)
@Rakotoarisoa try this one. i hope this will work for you
– Faraz
Nov 9 at 5:42
Thanks, it works. And I made a mistake to put the declare outside of the begin
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:11
add a comment |
up vote
0
down vote
In SQL SELECT SCOPE_IDENTITY()
Return Recent add record Identity Key in your case command_id
CREATE PROCEDURE dbo.spSetCommand
@Client_id INT,
@BcNumber INT,
@ArticleId INT,
@EntryNumber DECIMAL
AS
DECLARE @CommandId INT
BEGIN
INSERT INTO commands(client_id, bc_number, date_command)
VALUES (@Client_id, @BcNumber, GETDATE());
SET @CommandId = SELECT SCOPE_IDENTITY()
INSERT INTO entries(command_id, article_id, entry_number)
VALUES (@CommandId, @ArticleId, @EntryNumber);
END
There is an error in the below line code:
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
if there are multiples records exists WHERE bc_number = @BcNumber
then this will generate an error other wise working fine.
Atrenate way is below:
SELECT @CommandId =command_id commands WHERE bc_number = @BcNumber;
but the most appropriate way is to get recent added record in current table by current instant you can use SCOPE_IDENTITY()
SQL function
That's right thanks for your answer.
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:14
add a comment |
up vote
0
down vote
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
Instead try
SELECT @CommandId = top 1 command_id
FROM commands
WHERE bc_number = @BcNumber order by command_id desc;
I use set @foo = some int like 4; and select @foo = some query;
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
-- need to have brackets
set @CommandId = (SELECT command_id FROM commands WHERE bc_number = @BcNumber);
if there are multiple results from the query, this will fail, an error will come.
you can also use this:
SELECT @CommandID = command_id
FROM commands
WHERE bc_number = @BcNumber
In case of multiple results, this will give you the last value (which may/may not be your logical thing)
@Rakotoarisoa try this one. i hope this will work for you
– Faraz
Nov 9 at 5:42
Thanks, it works. And I made a mistake to put the declare outside of the begin
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:11
add a comment |
up vote
4
down vote
accepted
-- need to have brackets
set @CommandId = (SELECT command_id FROM commands WHERE bc_number = @BcNumber);
if there are multiple results from the query, this will fail, an error will come.
you can also use this:
SELECT @CommandID = command_id
FROM commands
WHERE bc_number = @BcNumber
In case of multiple results, this will give you the last value (which may/may not be your logical thing)
@Rakotoarisoa try this one. i hope this will work for you
– Faraz
Nov 9 at 5:42
Thanks, it works. And I made a mistake to put the declare outside of the begin
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:11
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
-- need to have brackets
set @CommandId = (SELECT command_id FROM commands WHERE bc_number = @BcNumber);
if there are multiple results from the query, this will fail, an error will come.
you can also use this:
SELECT @CommandID = command_id
FROM commands
WHERE bc_number = @BcNumber
In case of multiple results, this will give you the last value (which may/may not be your logical thing)
-- need to have brackets
set @CommandId = (SELECT command_id FROM commands WHERE bc_number = @BcNumber);
if there are multiple results from the query, this will fail, an error will come.
you can also use this:
SELECT @CommandID = command_id
FROM commands
WHERE bc_number = @BcNumber
In case of multiple results, this will give you the last value (which may/may not be your logical thing)
answered Nov 9 at 0:11
Gauravsa
1,5721816
1,5721816
@Rakotoarisoa try this one. i hope this will work for you
– Faraz
Nov 9 at 5:42
Thanks, it works. And I made a mistake to put the declare outside of the begin
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:11
add a comment |
@Rakotoarisoa try this one. i hope this will work for you
– Faraz
Nov 9 at 5:42
Thanks, it works. And I made a mistake to put the declare outside of the begin
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:11
@Rakotoarisoa try this one. i hope this will work for you
– Faraz
Nov 9 at 5:42
@Rakotoarisoa try this one. i hope this will work for you
– Faraz
Nov 9 at 5:42
Thanks, it works. And I made a mistake to put the declare outside of the begin
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:11
Thanks, it works. And I made a mistake to put the declare outside of the begin
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:11
add a comment |
up vote
0
down vote
In SQL SELECT SCOPE_IDENTITY()
Return Recent add record Identity Key in your case command_id
CREATE PROCEDURE dbo.spSetCommand
@Client_id INT,
@BcNumber INT,
@ArticleId INT,
@EntryNumber DECIMAL
AS
DECLARE @CommandId INT
BEGIN
INSERT INTO commands(client_id, bc_number, date_command)
VALUES (@Client_id, @BcNumber, GETDATE());
SET @CommandId = SELECT SCOPE_IDENTITY()
INSERT INTO entries(command_id, article_id, entry_number)
VALUES (@CommandId, @ArticleId, @EntryNumber);
END
There is an error in the below line code:
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
if there are multiples records exists WHERE bc_number = @BcNumber
then this will generate an error other wise working fine.
Atrenate way is below:
SELECT @CommandId =command_id commands WHERE bc_number = @BcNumber;
but the most appropriate way is to get recent added record in current table by current instant you can use SCOPE_IDENTITY()
SQL function
That's right thanks for your answer.
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:14
add a comment |
up vote
0
down vote
In SQL SELECT SCOPE_IDENTITY()
Return Recent add record Identity Key in your case command_id
CREATE PROCEDURE dbo.spSetCommand
@Client_id INT,
@BcNumber INT,
@ArticleId INT,
@EntryNumber DECIMAL
AS
DECLARE @CommandId INT
BEGIN
INSERT INTO commands(client_id, bc_number, date_command)
VALUES (@Client_id, @BcNumber, GETDATE());
SET @CommandId = SELECT SCOPE_IDENTITY()
INSERT INTO entries(command_id, article_id, entry_number)
VALUES (@CommandId, @ArticleId, @EntryNumber);
END
There is an error in the below line code:
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
if there are multiples records exists WHERE bc_number = @BcNumber
then this will generate an error other wise working fine.
Atrenate way is below:
SELECT @CommandId =command_id commands WHERE bc_number = @BcNumber;
but the most appropriate way is to get recent added record in current table by current instant you can use SCOPE_IDENTITY()
SQL function
That's right thanks for your answer.
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:14
add a comment |
up vote
0
down vote
up vote
0
down vote
In SQL SELECT SCOPE_IDENTITY()
Return Recent add record Identity Key in your case command_id
CREATE PROCEDURE dbo.spSetCommand
@Client_id INT,
@BcNumber INT,
@ArticleId INT,
@EntryNumber DECIMAL
AS
DECLARE @CommandId INT
BEGIN
INSERT INTO commands(client_id, bc_number, date_command)
VALUES (@Client_id, @BcNumber, GETDATE());
SET @CommandId = SELECT SCOPE_IDENTITY()
INSERT INTO entries(command_id, article_id, entry_number)
VALUES (@CommandId, @ArticleId, @EntryNumber);
END
There is an error in the below line code:
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
if there are multiples records exists WHERE bc_number = @BcNumber
then this will generate an error other wise working fine.
Atrenate way is below:
SELECT @CommandId =command_id commands WHERE bc_number = @BcNumber;
but the most appropriate way is to get recent added record in current table by current instant you can use SCOPE_IDENTITY()
SQL function
In SQL SELECT SCOPE_IDENTITY()
Return Recent add record Identity Key in your case command_id
CREATE PROCEDURE dbo.spSetCommand
@Client_id INT,
@BcNumber INT,
@ArticleId INT,
@EntryNumber DECIMAL
AS
DECLARE @CommandId INT
BEGIN
INSERT INTO commands(client_id, bc_number, date_command)
VALUES (@Client_id, @BcNumber, GETDATE());
SET @CommandId = SELECT SCOPE_IDENTITY()
INSERT INTO entries(command_id, article_id, entry_number)
VALUES (@CommandId, @ArticleId, @EntryNumber);
END
There is an error in the below line code:
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
if there are multiples records exists WHERE bc_number = @BcNumber
then this will generate an error other wise working fine.
Atrenate way is below:
SELECT @CommandId =command_id commands WHERE bc_number = @BcNumber;
but the most appropriate way is to get recent added record in current table by current instant you can use SCOPE_IDENTITY()
SQL function
answered Nov 9 at 5:42
Faraz
519311
519311
That's right thanks for your answer.
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:14
add a comment |
That's right thanks for your answer.
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:14
That's right thanks for your answer.
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:14
That's right thanks for your answer.
– Armand Mamitiana Rakotoarisoa
Nov 9 at 22:14
add a comment |
up vote
0
down vote
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
Instead try
SELECT @CommandId = top 1 command_id
FROM commands
WHERE bc_number = @BcNumber order by command_id desc;
I use set @foo = some int like 4; and select @foo = some query;
add a comment |
up vote
0
down vote
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
Instead try
SELECT @CommandId = top 1 command_id
FROM commands
WHERE bc_number = @BcNumber order by command_id desc;
I use set @foo = some int like 4; and select @foo = some query;
add a comment |
up vote
0
down vote
up vote
0
down vote
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
Instead try
SELECT @CommandId = top 1 command_id
FROM commands
WHERE bc_number = @BcNumber order by command_id desc;
I use set @foo = some int like 4; and select @foo = some query;
SET @CommandId = SELECT command_id
FROM commands
WHERE bc_number = @BcNumber;
Instead try
SELECT @CommandId = top 1 command_id
FROM commands
WHERE bc_number = @BcNumber order by command_id desc;
I use set @foo = some int like 4; and select @foo = some query;
answered Nov 9 at 6:48
Brian White
1,22711115
1,22711115
add a comment |
add a comment |
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%2f53217970%2fselect-and-insert-in-sql-server%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
I've never seen a DECLARE before BEGIN. Not sure if that might be your issue
– Brian White
Nov 9 at 6:44