-3

I have a table in SQL Server and I have written some custom query and got the result. Now I want to save the result into a new table. How can I do that? Below is my SQL query. I want to store the result data into a new table.

SELECT  
    Name, InvoiceNumber, InvoiceDate, Total,
    IIF(Name = '', '0', '100') AS Vendor_Recognition,
    IIF(InvoiceNumber = '', '0', '100') AS InvoiceNumber_Recognition,
    IIF(InvoiceDate = '', '0', '100') AS InvoiceDate_Recognition,
    IIF(Total = '', '0', '100') AS Total_Recognition
FROM 
    Invoice
3
  • @KoushikRoy: this is not supported in T-SQL / SQL Server. See the answer by Tyron down below as to how to do it in SQL Server Commented Jun 8, 2021 at 6:39
  • 1
    Does this answer your question? Create a table with SQL from Query result in SSMS Commented Jun 8, 2021 at 6:59
  • sorry, my bad. dint notice the tag. deleting my answer. Commented Jun 8, 2021 at 7:08

5 Answers 5

3

What about a simple SELECT...INTO?

SELECT  Name, InvoiceNumber, InvoiceDate, Total,
        IIF(Name = '', '0', '100') AS Vendor_Recognition,
        IIF(InvoiceNumber = '', '0', '100') AS InvoiceNumber_Recognition,
        IIF(InvoiceDate = '', '0', '100') AS InvoiceDate_Recognition,
        IIF(Total = '', '0', '100') AS Total_Recognition
    INTO MyNewTable
    FROM Invoice
Sign up to request clarification or add additional context in comments.

Comments

0

You can use SELECT... INTO.. syntax.

SELECT  
    Name, InvoiceNumber, InvoiceDate, Total,
    IIF(Name = '', '0', '100') AS Vendor_Recognition,
    IIF(InvoiceNumber = '', '0', '100') AS InvoiceNumber_Recognition,
    IIF(InvoiceDate = '', '0', '100') AS InvoiceDate_Recognition,
    IIF(Total = '', '0', '100') AS Total_Recognition
INTO MyNewTable
FROM 
    Invoice

also make sure you provide enough length in case if you want to insert more queries in same table

SELECT  
        Name, InvoiceNumber, InvoiceDate, Total,
        IIF(Name = '', '0', '100') AS Vendor_Recognition,
        IIF(InvoiceNumber = '', '0', '100') AS InvoiceNumber_Recognition,
        IIF(InvoiceDate = '', '0', '100') AS InvoiceDate_Recognition,
        cast(IIF(Total = '', '0', '100')) as Decimal(10,2) AS Total_Recognition  -- as this way you can define data type for MyNewTable
    INTO MyNewTable
    FROM 
        Invoice

Comments

0
SELECT  
    Name, InvoiceNumber, InvoiceDate, Total,
    IIF(Name = '', '0', '100') AS Vendor_Recognition,
    IIF(InvoiceNumber = '', '0', '100') AS InvoiceNumber_Recognition,
    IIF(InvoiceDate = '', '0', '100') AS InvoiceDate_Recognition,
    IIF(Total = '', '0', '100') AS Total_Recognition
INTO MyInvoice
FROM 
    Invoice

I have written query to store your result in table named 'MyInvoice'. Please note below query is not re-runnable and if you want to make it re-runnable you have to drop the table first and then execute the query given above

Comments

-1

The simplest way to insert data from an existing table is Insert Into:

INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1
WHERE condition;

You can find elaborting information here: SQL INSERT INTO SELECT Statement

PS: Wow just saw CREATE TABLE new_table AS (SELECT * FROM old_table); in a comment. That is syntax i did not know yet. Thank you Create Table (with as explaination)

CREATE TABLE TestTable AS
SELECT customername, contactname
FROM customers;

1 Comment

CREATE TABLE ... AS SELECT ..... is not supported in T-SQL/SQL Server. You need to use SELECT (list of columns) INTO NewTableName FROM ..... instead
-1

I think creating the table first and then inserting the result will be a good idea as it will give us an option to define indexes at the time of table creation which will be helpful in performance and catching. Thoughts?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.