This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Multiple Select Statements using SQL Server 2005 “WITH” Statement
There's any way to get multiple result-sets from a query using WITH clause?
I'm using MS SQL 2005.
;with temp as
(
SELECT '1' as [Sample]
)
--SELECT COUNT(*) FROM temp
SELECT * FROM temp
This works with each select (count or *), but I need to return both result-sets from the same "temp".
Is that possible?
Use UNION ALL:
;with temp as
(
SELECT '1' as [Sample]
)
SELECT COUNT(*) FROM temp
UNION ALL
SELECT * FROM temp
I'd suggest you need to re-work whatever was meant to consume this pair of result sets so that it doesn't need to know how many results are going to be returned before the results arrive. If that's not the case, I can't think of where else you'd need to retrieve the COUNT of the result set as well as the result set itself.
Related
This question already has answers here:
How to use multiple CTEs in a single SQL query?
(4 answers)
Closed 5 years ago.
I'm trying to do something like:
WITH megalodon_view AS (
-- 200 lines of gibberish
)
WITH RECURSIVE traverse_table AS (
-- big queries with multiple uses of megalodon_view for recursive traversing
)
Observe it's not a mather of defining 2 CTEs, but to use on within the scope of the other.
I'd like to use it in production, so I want preferrably not to create anything physically
You can specify this as:
WITH recursive megalodon_view AS (
-- 200 lines of gibberish
),
traverse_table AS (
-- big queries with multiple uses of megalodon_view for recursive traversing
)
select . . .;
The with recursive is needed only once, for the first CTE (even though that one is not recursive).
Without exact query it is hard to say what are you trying to achieve. Anyway you could nest WITH like:
WITH RECURSIVE t(n) AS (
SELECT *
FROM (WITH cte AS (SELECT 1 )
SELECT * FROM cte
) sub
UNION ALL
SELECT n+1 FROM t WHERE n < 100
)
SELECT COUNT(*) FROM t;
DBFiddle Demo
I have a big select with many inner join and left join and select to select like:
SELECT
( ... )
ORDER BY
Price
The question is for count of select am I must to run this select again ?
SELECT
COUNT( ... )
ORDER BY
Price
Is there any easy way to run one times and get result of select and count of select ?
Here is my C# code with Entity Framework:
string strQuery = "....";
IQueryable<ProductDto> list = _entities.Database.SqlQuery<ProductDto>(strQuery).AsQueryable();
You can use select ##rowcount to get the number of rows selected by the previous statement as a second resultset, or use SET #myOutputVar = ##ROWCOUNT it as an output parameter of your stored procedure.
A few Q/A's here on StackOverflow that may also help
ADO.NET number of rows that may be returned by a SQL Server select stored procedure
How to get number of rows using SqlDataReader in C#
Yes you can try select ##rowcount.It Returns the number of rows affected by the last statement. If the number of rows is more than 2 billion, use ROWCOUNT_BIG.
https://msdn.microsoft.com/en-IN/library/ms187316.aspx
This question already has answers here:
Is it possible to have a tableless select with multiple rows?
(3 answers)
Closed 7 years ago.
I have some data that is hard coded in my select query.
The SQL is as follows:
SELECT
'ZZ0027674',
'ZZ0027704',
'ZZ0027707',
'ZZ0027709',
'ZZ0027729',
'ZZ0027742',
'ZZ0027750'
Unfortunately it does not display the data. It just returns 7 columns and each column has each value. I just want 1 column with the different values.
Please provide me different solutions to display the data?
You can use VALUES, aka Table Value Constructor, clause for hardcoded values:
SELECT *
FROM (VALUES('ZZ0027674'),('ZZ0027704'),('ZZ0027707'),
('ZZ0027709'),('ZZ0027729'),('ZZ0027742'),
('ZZ0027750')
) AS sub(c)
LiveDemo
Warning: This has limitation up to 1000 rows and applies to SQL Server 2008+. For lower version you could use UNION ALL instead.
EDIT:
Extra points if someone can show me unpivot ?
SELECT col
FROM (SELECT 'ZZ0027674','ZZ0027704','ZZ0027707',
'ZZ0027709','ZZ0027729','ZZ0027742','ZZ0027750'
) AS sub(v1,v2,v3,v4,v5,v6,v7)
UNPIVOT
(
col for c in (v1,v2,v3,v4,v5,v6,v7)
) AS unpv;
LiveDemo2
Use union:
SELECT
'ZZ0027674' union all
SELECT 'ZZ0027704' union all
SELECT 'ZZ0027707' union all
SELECT 'ZZ0027709' union all
SELECT 'ZZ0027729' union all
SELECT 'ZZ0027742' union all
SELECT 'ZZ0027750'
Can also use union
SELECT 'ZZ0027674' as [col1]
UNION
SELECT 'ZZ0027704'
This question already has answers here:
Oracle: Using Pseudo column value in the same Select statement
(3 answers)
Closed 9 years ago.
Folks I found similar but not exact questions on this forum - pardon me if i have not done enough searching for them. This is my question..in Oracle
select ( t.value*2) as inst2, (inst2 * 3) as inst3
from table t;
the thinking behind is if f() = t.value*2 is an expensive call, then we do not need to make that twice..or is there an alternative query structure i could use (I am trying to achieve this in CTAS)
thanks in advance.
Another option:
with cte as (
select t.value*2 as inst2
)
select
cte.inst2,
(cte.inst2*3) as inst3
from cte
This is actually the same as in bluefeet's reply, but I would consider it easier to understand with the with-syntax.
If you want to use an alias in the second calculation, then you will want to use a subquery:
select inst2,
(inst2 * 3) as inst3
from
(
select t.value*2 as inst2
from table t
)
I want to check my database for records that I already have recorded before making a web service call.
Here is what I imagine the query to look like, I just can't seem to figure out the syntax.
SELECT *
FROM (1,2,3,4) as temp_table
WHERE temp_table.id
LEFT JOIN table ON id IS NULL
Is there a way to do this? What is a query like this called?
I want to pass in a list of id's to mysql and i want it to spit out the id's that are not already in the database?
Use:
SELECT x.id
FROM (SELECT #param_1 AS id
FROM DUAL
UNION ALL
SELECT #param_2
FROM DUAL
UNION ALL
SELECT #param_3
FROM DUAL
UNION ALL
SELECT #param_4
FROM DUAL) x
LEFT JOIN TABLE t ON t.id = x.id
WHERE x.id IS NULL
If you need to support a varying number of parameters, you can either use:
a temporary table to populate & join to
MySQL's Prepared Statements to dynamically construct the UNION ALL statement
To confirm I've understood correctly, you want to pass in a list of numbers and see which of those numbers isn't present in the existing table? In effect:
SELECT Item
FROM IDList I
LEFT JOIN TABLE T ON I.Item=T.ID
WHERE T.ID IS NULL
You look like you're OK with building this query on the fly, in which case you can do this with a numbers / tally table by changing the above into
SELECT Number
FROM (SELECT Number FROM Numbers WHERE Number IN (1,2,3,4)) I
LEFT JOIN TABLE T ON I.Number=T.ID
WHERE T.ID IS NULL
This is relatively prone to SQL Injection attacks though because of the way the query is being built. It'd be better if you could pass in '1,2,3,4' as a string and split it into sections to generate your numbers list to join against in a safer way - for an example of how to do that, see http://www.sqlteam.com/article/parsing-csv-values-into-multiple-rows
All of this presumes you've got a numbers / tally table in your database, but they're sufficiently useful in general that I'd strongly recommend you do.
SELECT * FROM table where id NOT IN (1,2,3,4)
I would probably just do:
SELECT id
FROM table
WHERE id IN (1,2,3,4);
And then process the list of results, removing any returned by the query from your list of "records to submit".
How about a nested query? This may work. If not, it may get you in the right direction.
SELECT * FROM table WHERE id NOT IN (
SELECT id FROM table WHERE 1
);