How can we use Multiple Queries using 'WITH' Clause - sql

as sample code below....
WITH sampleA as (SELECT * FROM emp)
SELECT * FROM sampleA
SELECT * FROM sampleA
this alias 'sampleA' will work for only first query not for second or later..
But I want to query more with this alias only.
Can you please help me, how can I do that?

Common table Expression scope is limited to first SELECT statement. For multiple usage , use instead temporay table or table variable.

Related

T-SQL SELECT statement

I have a simple question regarding SELECT statement in SQL Server. I would like to know the purpose of the following syntax:
SELECT column_name, . *
I don't understand the purpose of the (period) and a (star) after the SELECT. I understand SELECT column_name1, column_name2,.... etc. or SELECT *...
but what does a period do before the star.
That is invalid syntax and will not run.
.* can be used following a table name or alias to get all columns for that table. For example...
SELECT mytable.* FROM mytable
or
SELECT a.column_one, a.* FROM mytable a

SQL alias select

I need to reorder my query to have the name in the first column, but I still need the rest of the columns populated as well.
How would you implement an alias in oracle to achieve this query?
select s.name, s.* from table as s
Thanks for the help.
There is no simple way around this, you must specify all columns in the order you would like them.
select s.name, s.column1, s.column2, ... from table as s

SQL use column name alias without SELECTING

I know I can specify a column alias like so:
SELECT stuff as mask
Is there a way I can specify a column alias without returning that column data in the result set? Essentially, I want to be able to make my query clean by doing:
SELECT doManipulationStuff(cleanCompactAlias)
Where
reallyLong.misleading.andAnnoying.columnName as cleanCompactAlias
You can use a Common Table Expression (CTE) to create a subquery with aliases:
WITH clean_cte AS
(
SELECT reallyLong.misleading.andAnnoying.columnName1 as cleanCompactAlias1,
reallyLong.misleading.andAnnoying.columnName2 as cleanCompactAlias2
)
SELECT doManipulationStuff(cleanCompactAlias1),
doManipulationStuff(cleanCompactAlias2)
That way you can put all of your aliasing in the CTE and just forget about it when calling your functions. You still have to do the aliasing somewhere, but this at least keeps it out of your main query to make it a bit more readable.
You could use a subquery:
select doManipulationStuff(cleanCompactAlias)
from (select t.*, reallyLong.misleading.andAnnoying.columnName as cleanCompactAlias
. . .
) t

Not Able to Query Multiple Times from Multiple Common Table Expressions (WITH)?

I was doing some querying today in T-SQL, SQL-Server-2008 and stumbled upon something weird that I didn't understand. Using the query windows, I am trying to query from two common table expressions like so (I stripped out a lot of code to make it more obvious what I was doing):
;WITH temp1 AS (SELECT * FROM dbo.Log)
, temp2 AS (SELECT * FROM dbo.SignalCodeItems300_tbl)
SELECT * FROM temp1
SELECT * FROM temp2
However, only one of the select statements will run, the FIRST one. Regardless of which is which, only the first runs. I assume this is some sort of syntax thing that I'm missing maybe? I get the error "Invalid object name 'temp2'".
Could someone shed some light on this problem? Are there any workarounds for this?
No, this works as it should. A CTE (Common Table Expression) is only available for the first statement after the definition. So in other words, after select * from temp1, they both become unavailable.
The fix would be this:
;WITH temp1 AS (SELECT * FROM dbo.Log)
SELECT * FROM temp1
;WITH temp2 AS (SELECT * FROM dbo.SignalCodeItems300_tbl)
SELECT * FROM temp2
You might want to take a look at the MSDN documentation.
Especially:
Multiple CTE query definitions can be defined in a nonrecursive CTE.
The definitions must be combined by one of these set operators:
UNION ALL, UNION, INTERSECT, or EXCEPT.
You cannot mix and match two different schemas though, as this essentially runs as one query.
Use a view or a user-defined, table-valued function to house your query if you don't want to repeat it explicitly.

Can where clause condition be represented by another column

I have users enter the condistions for where clause for a table. Now I want to use that clause to do select. How can i do that? Example,
Condition table ( ckey, condition)
1 fn like 'G%' and ln like 'B%'
Name table (nkey, fn, ln)
Query wanted
select * from Name where ... use condition in row 1 of Condition table .....
Most DBMSes support a subquery inside WHERE, including correlated subquery.
It's hard to say without more info, but you'll probably need a (correlated?) subquery on Condition table.
One possible option would be to setup your condition table to mimic the table you're searching on. Then you could simply join the tables on all fields. In order for this to work you would have to default all columns in the condition table to '%' in case that condition isn't specified.
If your requirements are too complex for that to work, then another solution would be to use a stored procedure that generates dynamic sql based on what's in the condition table.
See below
accept myID prompt "Enter ID : "
SELECT * FROM myTable WHERE id='&myID'
OR
SELECT * FROM myTable WHERE id= #Enter_ID)
see which one is working with you...