How to do Query in Sql Server

Nona Mohammed 41 Reputation points
2021-09-08T07:56:22.127+00:00

Hi...
I have a problem .... I'm trying to execute This Querry but give me an error

insert into GenStoTable(ProductID,StID,Quantity,LowQ)(select (select id from ProductTable),max(ID),0,0 from StrNamTable)

I'm trying to use insert into with select from 2 tables .... please help me ... and thank's alot

Developer technologies | Transact-SQL
Developer technologies | Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Viorel 125.9K Reputation points
    2021-09-08T08:03:11.713+00:00

    Try removing "( )":

    insert into GenStoTable(ProductID, StID, Quantity, LowQ)
    select (select id from ProductTable), max(ID), 0, 0 
    from StrNamTable
    

    It assumes that ProductTable has a single row, otherwise it cannot work.

    Give details if you need something different.


1 additional answer

Sort by: Most helpful
  1. MelissaMa-msft 24,246 Reputation points Moderator
    2021-09-08T08:39:49.093+00:00

    Hi @Nona Mohammed ,

    the final result that need it
    GenStoTable ProductID StID Quantity LowQ
    4 3 0 0
    5 3 0 0
    6 3 0 0
    7 3 0 0

    Please refer below:

     insert into GenStoTable(ProductID,StID,Quantity,LowQ)  
     select id ,(select max(id) from StrNamTable),0,0 from ProductTable  
    

    Output:

    ProductID	StID	Quantity	LowQ  
    4	3	0	0  
    5	3	0	0  
    6	3	0	0  
    7	3	0	0  
    

    Best regards,
    Melissa


    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.