CTE inside a subquery - sql

Is SQL Server capable of having a CTE inside a subquery? I have not seen it.
Started using Oracle and saw a developer use CTE inside a subquery. Is SQL Server capable of it? I'm testing it on my own but it doesn't work in SQL Server. Let me know if CTE works inside a subquery.
Thanks

Related

Convert a Recursive CTE in SQL Server to netezza

I have a requirement like i need to convert a recursive CTE in sql server into netezza !
Will netezza even support Recursive CTE ?
Can anyone help me on this ?
Thanks in advance,
Manirathinam.
Recursive CTEs are not supported in Netezza as of version 7.2. If you can tell us what you are trying to accomplish in your particular case, we might be able to offer a workaround/rewrite.

Simple SQL query that works for Oracle and SQL Server

I am looking for a small and simple query that works on Oracle and SQL Server.
It is used as a test query to check the connection.
For SQL Server we used SELECT 1, but in Oracle it would have to be SELECT 1 FROM DUAL.
What we plan to use now is SELECT COUNT(*) FROM (sometable) but any ideas for an even simpler query are appreciated.
One simple option is to add a view to SQL Server called DUAL that just returns 1, that way you can have a simple query that works the same in both environments:
SELECT 1 FROM DUAL
If returning data from relations isn't important then:
SELECT 'Hello world';
will rely on a connection as much as anything else. Your RDBMS should return 'Hello world'. Tested on SQL server and PostgreSQL (don't have access to Oracle).
As long as you write query in ANSI standard. It can be executed in all the RDMS.
May be you can try this query....
select ColumnName from TableName where 1=2
BTW, the DBProvider shld have come property to state of DB connectivity... which DB provider does ur application uses?
Does it need to be a query? You could create a simple stored procedure with the same name in both databases, that just returns a constant, and execute it from the application server.

Adaptation of the merge clauses in SQL Server 2005

I have a code which is up and running but the problem that I have is that it includes a lot of MERGE clauses as it was intended to be run from SQL Server 2008 and forward. But the problem is that a new customer is running SQL Server 2005 and as you know the Merge clause is not available till SQL Server 2008, so my question is if there is not a way to parse the this clauses automatically or if there is another solution (apart from rewriting all the clauses in clasical clauses ) as the customer is not willing to upgrade the DB.
Thanks a lot in advance.
I am afraid that you will need to re-write all of your MERGE clauses for SQL Server 2005.
You can use the 2005 friendly output clause to achieve the same functionality but with more verbose SQL. This approach will also work on SQL Server 2008.
http://sqlserver-tips.blogspot.co.uk/2006/09/mimicking-merge-statement-in-sql.html
You could put together a C# (or whatever) app to parse the merge statements and create insert/update statements from them. I mean, that's a horrible idea, but you can do it...
You'd have to pull out the "ON" part, add it to a check IF EXISTS and the update part, then add the column list to the insert part. You could even programmatically (sic?) create SP params and everything.
Heh. Good luck.

updating temp table from linked server using top clasue

I am trying to update a #temp table from a linked server using a top clause but am getting a very odd result - it seems to be ignoring the order by in my code.
I can fix the specific problem programatically but would like to know if this is a one off issue or there is a general problem with the way i have linked the servers.
The query is being run on a 2005 SQL Server (9.0.3042) linked to a 2008 SQL Server R2 (10.50.279) linked via the Microsoft OLE DB Provider for SQL Server.
The query looks like this - i have already created #TempTable which has the columns Id, Date and PrevDate and inserted data into the Id and Date columns.
update #TempTable
set PrevDate =
(select top 1
d.Date
from
linkedserver.DB.dbo.Date as d
where
d.Id = #TempTable.Id
and d.Date < #TempTable.Date
order by
d.Date desc)
The select is not picking the top 1, it appears to be picking the first date entered into the table for the specific Id and ignoring the order by clause.
When I just do a select, it works fine
When I put in the PrevDate via the inital insert, it works fine.
When I use a Max rather than a Top, it works fine.
When I run the exact same query/scenario on the 2008 SQL Server via a link (same provider) back to the 2005 SQL Server, it works fine
I am not looking for a fix for this specific query but would like to know if this is an isolated incedent or some fundamental problem with 2005 to 2008 linking that is going to manifest itself in very hard to find ways.
Thanks,
Tim
I have some bad news. I have replicated this issue here. It looks to me to be an issue with SQL Server 2005's query plan (I tested with a later version too, v 9.0.4035):
Image: Query plan for update statement
(sorry I can't embed images here, not enough reputation)
The details for that remote query:
Image: Remote Query details
There is no ORDER BY statement sent to the remote server, so the results that come back are indeed just the TOP (1) in an arbitrary order, which is likely to be in the order of the primary key. It seems to me that SQL Server should have emitted an ORDER BY Date desc there.
Therefore, I have to conclude that other queries like this, in this environment, could easily have the same issue (so, not an isolated incident).
By the way, there is a Microsoft Connect issue already raised for this one, from 2009: connect.microsoft.com/SQLServer/feedback/details/446017/missing-order-by-in-remote-query-with-update-statement

T-SQL cross server function execution -Is there a configuration in SQL server to execute functions cross servers?

I have a function in [MHL1P].[x].dbo.udf_Admin_GetNameByID(t3.StaffID)
I want to execute this function from another server in a store procedure like this:
select [MHL1P].[x].dbo.udf_Admin_GetNameByID(StaffID)
from StaffIdTable
Is there a configuration in SQL server to execute functions cross servers?
Look at sp_addlinkedserver
Note that this is usually a bad idea, as certain things don't work as well on linked servers (the query optimizer doesn't have good information about statistics or indexes, for example).
Assuming you have a linked server you may need to use OPENQUERY to call a UDF as this article suggests.
I'm not sure if they fixed this in 2005 or 2008.