Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming DataSet.table after performing SQLCommand (Select) Query

In stored procedure MS SQL My query is:

SELECT *  
FROM ContentReportRequests a,UserPreferences d
WHERE  a.UserID = d.UserID and a.ID =@ID

I want to give the result table some name. How can I do this ?

I want to pull it to ADO.Net DataSet.tables["NAME"]

like image 646
Mark T Avatar asked Apr 21 '11 07:04

Mark T


People also ask

How do you name a SQL results table?

When naming tables, you have two options – to use the singular for the table name or to use a plural. My suggestion would be to always go with names in the singular. If you're naming entities that represent real-world facts, you should use nouns. These are tables like employee, customer, city, and country.

Which query will you use to fetch ID and name?

The SQL SELECT statement is used to fetch the data from a database table which returns this data in the form of a result table.

Which clause will select rows from a database table?

The WHERE clause selects only the rows in which the specified column contains the specified value.


1 Answers

I can imagine a few things you might be meaning.

If you want to persist this result set, for consumption in multiple later queries, you might be looking for SELECT INTO:

SELECT * into NewTableName
FROM ContentReportRequests a,UserPreferences d
WHERE  a.UserID = d.UserID and a.ID =@ID

Where NewTableName is a new name, and a new (permanent) table will be created. If you want that table to go away when you're finished, prefix the name with a #, to make it a temp table.

Alternatively, you might just be wanting to absorb it into a single larger query, in which case you'd be looking at making it a subselect:

SELECT *
FROM (SELECT *  
FROM ContentReportRequests a,UserPreferences d
WHERE  a.UserID = d.UserID and a.ID =@ID
) NewTableName
WHERE NewTableName.ColumnValue = 'abc'

or a CTE:

WITH NewTableName AS (
    SELECT *  
    FROM ContentReportRequests a,UserPreferences d
    WHERE  a.UserID = d.UserID and a.ID =@ID
)
SELECT * from NewTableName

Finally, you might be talking about pulling the result set into e.g. an ADO.Net DataTable, and you want the name to be set automatically. I'm not sure that that is feasible.

like image 57
Damien_The_Unbeliever Avatar answered Nov 16 '22 03:11

Damien_The_Unbeliever