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

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,097 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,580 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 113.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,176 Reputation points
    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.