1

I have a select query that I am using in Access to fetch some particular data that I want to achieve. Now I am moving to SQL Server (creating ssrs report) so I want to take this query from Access and use it in SQL Server and create a new table every time when I execute this query.

Here is my current query that I use in Access db:

select taxid, address1, count(address1)
from dbo.tblaccounts
group by taxid, address1
order by address1 asc, count(address1) desc;

Where should I insert/into statement...or is that something else that I need here.

2
  • 2
    In between the select and from clause add INTO TableNameHere, is that what you are asking? Commented Apr 17, 2017 at 14:12
  • I've put an answer below, but if I'm off target, please be careful creating tables whenever a report is run: These should be "temp tables" otherwise if two people run the report at the two operations will interfere with one another. In SQL-Server, you make a temp table in the same way as any other, but the name begins with a #. Commented Apr 17, 2017 at 16:09

4 Answers 4

5

What you are trying to perform is called Select Into or Create Table As Select (CTAS). MSDN has a great discussion of when to use CTAS vs. Select Into (https://learn.microsoft.com/en-us/azure/sql-data-warehouse/sql-data-warehouse-develop-ctas). Though this article discusses data warehousing, the discussion is valid in this case, IMO.

As most of my table creation from another table involve both complex joins and unions, I tend to use CTAS. That said, the Select Into method will likely work in your scenario.

The code you would use is either:

CREATE TABLE AccountSummaryOrWhatever AS
select taxid, address1, count(address1)
from dbo.tblaccounts
group by taxid, address1;

Or

select taxid, address1, count(address1)
INTO AccountSummaryOrWhatever 
from dbo.tblaccounts
group by taxid, address1;
Sign up to request clarification or add additional context in comments.

4 Comments

Is CTAS available outside of data warehousing editions of SQL Server? Otherwise a significant limitation is that CTAS is unavailable for many users of SQL Server.
I have used the CTAS method with Oracle 10g and 11i, MySql since version 5.5 and MS SQL since at least Server 2000. It is a standard practice and is not limited to data warehousing versions of the software.
You have not been using CTAS in SQL Server 2000, unless you mean SELECT INTO which isn't the same. CTAS was not available through at least 2008 R2 (based on experience) and per the documentation is not available on MS SQL Server through 2016 except "Azure SQL Data Warehouse" and "Parallel Data Warehouse" learn.microsoft.com/en-us/sql/t-sql/statements/…. Note the table of contents has only one "Table as select" entry that applies to warehouse editions.
I wasn't the DBA so I'm not sure the exact version. I was told it was 2000 by the ODBA when I asked. At that time I was stuck using FoxPro.
1

If you need to create a new table based on that query, there are multiple ways, the easiest way is use Select Into From, which the query will be:

select taxid, address1, count(address1)
INTO TABLEA
from dbo.tblaccounts
group by taxid, address1
order by address1 asc, count(address1) desc;

Note: this method only transfer the data, and constraints,etc. will not get preserved. BUT if you need to get that table every time you run the query, you need to DROP and create the table again, otherwise, there will throw the object error. Also, there might be lots of approaches. The simple way to do is:

IF EXISTS(select 1 from sys.objects where name = 'TABLEA' and type = 'U') --or you could use IF EXISTS(OBJECTID('TABLEA','U'))
BEGIN 
DROP TABLE TABLEA
END
--then paste your query after
    select taxid, address1, count(address1)
    INTO TABLEA
    from dbo.tblaccounts
    group by taxid, address1
    order by address1 asc, count(address1) desc;

Comments

1

I've got a hunch that what you really want to do is create a view. Queries in MS Access are like views (or in the case of ADD/DELETE/UPDATE queries they are like stored procedures). Just like MS Access queries, a view can be referenced by other code in the same way as a table and like an Access Query this acts like an "always up-to-date" table.

create view ViewName
as
select
    taxid,
    address1,
    count(address1) as address_count
from
    dbo.tblaccounts
group by
    taxid,
    address1

You'll notice that I've left off the ORDER BY clause. They aren't permitted here, you specify the order by in whatever code selects from this view.

2 Comments

Thank you all. If I want to add an other column with one other criteria like...: there is a column name 'cust_name' and I want to select DISTINCT 'cust_name' (customer name) than where would I add that line into the code? So, I result should give me the columns with taxid and distinct customer names. (address one is not really important and can leave the count out too, since my goal is to achieve unique records with taxid and customername). thanks so much
That sounds like an extra question, and is probably best submitted as a new question. You can reference this question in that one if it helps. In the meantime, if one of the answers here is what you needed, do mark it as accepted.
0
Create Procedure proc_new_table 
As
SET NOCOUNT ON;

IF OBJECT_ID('new_table','U') IS NOT NULL 
DROP TABLE new_table;
select distinct cust_name, taxid
INTO new_table
from tblaccounts;

Thank you all!

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.