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?










share|improve this question
























  • I've never seen a DECLARE before BEGIN. Not sure if that might be your issue
    – Brian White
    Nov 9 at 6:44















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?










share|improve this question
























  • I've never seen a DECLARE before BEGIN. Not sure if that might be your issue
    – Brian White
    Nov 9 at 6:44













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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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)






share|improve this answer





















  • @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


















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






share|improve this answer





















  • That's right thanks for your answer.
    – Armand Mamitiana Rakotoarisoa
    Nov 9 at 22:14


















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;






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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)






    share|improve this answer





















    • @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















    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)






    share|improve this answer





















    • @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













    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)






    share|improve this answer












    -- 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)







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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


















    • @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












    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






    share|improve this answer





















    • That's right thanks for your answer.
      – Armand Mamitiana Rakotoarisoa
      Nov 9 at 22:14















    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






    share|improve this answer





















    • That's right thanks for your answer.
      – Armand Mamitiana Rakotoarisoa
      Nov 9 at 22:14













    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






    share|improve this answer












    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







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 9 at 5:42









    Faraz

    519311




    519311












    • 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




    That's right thanks for your answer.
    – Armand Mamitiana Rakotoarisoa
    Nov 9 at 22:14










    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;






    share|improve this answer

























      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;






      share|improve this answer























        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;






        share|improve this answer












        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;







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 6:48









        Brian White

        1,22711115




        1,22711115






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Schultheiß

            Verwaltungsgliederung Dänemarks

            Liste der Kulturdenkmale in Wilsdruff