Share via

Need Query

hari prasad 6 Reputation points
2021-06-01T07:44:01.14+00:00

Create table #sales(salesamt int)
Insert into #sales values(100)
Insert into #sales values(200)
Insert into #sales values(300)

if Count #sales>0 then insert

Insert into #sales1(salesamt)
select sum(Salesamt) from #sales
else print 0

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.


1 answer

Sort by: Most helpful
  1. EchoLiu-MSFT 14,626 Reputation points
    2021-06-01T07:57:06.257+00:00

    Hi @hari prasad ,

    Welcome to the microsoft TSQL Q&A forum!

    If #sales1 has been created, then:

    IF (SELECT Count (*) FROM #sales)>0   
       INSERT INTO  
       #sales1  
       SELECT SUM(Salesamt)   
       FROM #sales  
    ELSE  
       PRINT 0  
    

    If #sales1 has not been created yet, then:

    IF (SELECT Count (*) FROM #sales)>0   
       SELECT SUM(Salesamt) ss   
       INTO #sales1  
       FROM #sales   
    ELSE  
       PRINT 0  
    

    If you have any question, please feel free to let me know.
    If the response is helpful, please click "Accept Answer" and upvote it.

    Regards
    Echo


    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.

    Was this answer helpful?

    0 comments No comments

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.