Work around temporary table in SQL Server 2008 view - sql-server-2008

I wrote a query in this format..
Select * INTO #xyz FROM ()
which I later want to use to create a view, as...
CREATE VIEW aaa
AS
Select * INTO #xyz FROM ()
but getting the following errors:
Incorrect syntax near the keyword 'INTO'.
Views or functions are not allowed on temporary tables
Can someone please suggest a workaround this? Is it possible to use temporary tables to create views?

You certainly cannot use a select into in a view. And a termp table is not approraite either. Use a derived table that is the equivalent of the temp table instead. Example:
Suppose you had:
select t1.id, t2.field1 into #temp from table1 t1
join Table2 t2 on t1.id = t2.id
where t2.somefield2 = 'mytest'
select t1.id, t2.field1, t1.field1
from mytable2 t1
join #Temp t2 on t1.id = t2.id
where t2.somefield = 'test'
Then you could use:
select t1.id, t2.field1, t1.field1
from mytable2 t1
join (select t1.id, t2.field1
from table1 t1
join Table2 t2 on t1.id = t2.id
where t2.somefield2 = 'mytest') t2
on t1.id = t2.id
where t2.somefield = 'test'
You could also usea a CTE

Just as the error message says it is not possible to use temp tables.
You should use a permanent table or a CTE which can also be specified in a view.
A CTE could help you out depending on your situation. Describe your problem with some context if you think it suitable after researching what a CTE is. In short a CTE is a query that you can reference multiple times which in the past people used temp tables for.
http://msdn.microsoft.com/en-us/library/ms190766(v=sql.105).aspx

Related

Convert query from MySQL to SQL Server (include GROUP BY)

I'm trying to convert this MySQL query to SQL Server, but I do not know much about SQL Server
SELECT
*
FROM
Table1 AS T1
INNER JOIN Table2 AS T2
ON T1.Column1 = T2.ColumnX
WHERE
T1.ColumnY = 'xxxx'
GROUP BY
T1.Column1
Somebody can help me?
Your query is just an erroneous query, because you are using select * with group by. This query uses a MySQL extension. And, the default settings in more recent versions of MySQL would generate an error.
Here is one method for converting this to SQL Server:
SELECT TOP (1) WITH TIES *
FROM Table1 AS T1 INNER JOIN
Table2 AS T2
ON T1.Column1 = T2.ColumnX
WHERE T1.ColumnY = 'xxxx'
ORDER BY ROW_NUMBER() OVER (PARTITION BY T1.Column1 ORDER BY (SELECT NULL)) ;
Probably a better method (from a performance perspective) uses a lateral join:
SELECT *
FROM Table1 T1 CROSS APPLY
(SELECT TOP (1) T2.*
FROM Table2 T2
WHERE T1.Column1 = T2.ColumnX
) T2
WHERE T1.ColumnY = 'xxxx' ;
Both of these choose arbitrary rows from Table2 when there is more than one match.

Create view by selecting different columns from multiple tables in SQL server

Im trying to create a view where specific Columns from 2 tables to be combined.
My Tables
Table1(Column1,Column2,Column3)
Table2(Column4,Column5,Column6)
Expected output
View1(Column2,Column3,Column6)
What query can i use to achieve that output?
CREATE VIEW [dbo].[View]
AS
SELECT a.Column2,a.Column3,b.Column6
FROM Table1 AS a
INNER JOIN Table2 AS b ON A.ID = B.ID -- your logic
If you have and id to join tables:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
join table2 t2 on t1.id = t2.id
If you don't have and id and want to get mixes columns without relationships:
create view View1
select t1.column2, t1.column3, t2.column6
from table1 t1
cross join table2 t2 on t1.id = t2.id
As Fourat and jarhl commented above, you need to explicitly define what columns are used to JOIN both tables. So using the below code change the ID column to the column that relates these two tables, and if it's more than one column the list them all separated by AND.
CREATE VIEW dbo.YourViewName
AS
SELECT t1.Column2, t1.Column3, t2.Column6
FROM Table1 t1
JOIN Table2 t2 ON t1.ID = t2.ID
GO

MySQL if join table.col exists case as part of the query itself?

I have a weird situation here that I try to resolve through SQL so I don't have to modify much in the application :).
Is there ANY way that I can tell if a column exists in a quere within the query?
(Table2 is not always joined in the query)
SELECT * FROM
Table1 as T1
join Table2 as T2 on t1.id = t2.fk
WHERE
T1.something > 10 OR (IF(table and column exists in the query T2.col, 1, 0);
my alternative is to always join Table2 (painful in this case).
Any SQL guru/genius can help?
I solved the problem with an easy work around. So, for the sake of referencing...
SELECT *
FROM Table1 as T1
WHERE T1.something > 10 OR ((SELECT ....) = 1)

Is this the correct way to search in MySQL

I have two tables. t1 and t2. I want to select the values that are from t1 and occurred in t2. What I wrote is the following:
select col1 from t1, t2
where t1.col1y=t2.col2;
Is this is the right way to search for the value ?
Yes, that works, but it's the old form of writing a join. Use the new form:
select t1.col1
from t1
inner join t2 on t1.col1 = t2.col2

SQL commands to check ID difference

I have 2 tables.
How do I know IDs that are in one table and not on the other table? How would I accomplish that?
And then I want to delete all such IDs.
This is pretty simple:
delete from t1
using table1 as t1
left outer join table2 as t2
on t1.id = t2.id
where t2.id is null
It's worth noting that joins are faster than subqueries.
Use this query:
delete from TABLE_A where ID not in (select ID from TABLE_B)
Use this query:
delete from t1
where id not in
(select t2.id from t2)