sql 2000 is'not executing the query that 2008 can execute - sql

If I execute the following SQL statement in SQL server 2008 it works perfectly, but when execute the same statement in SQL Server 2000 it doesn't work:
Statement:
select top 1
[k].[FixbiUnitPrice]
from (
select top (select COUNT(*)
from [dbo].[mnrFnBI_Fixed]('4E591E71-33BD-4ECC-8703-771BE8A76817') f)
[FixbiUnitPrice],
BDate,
biNumber
From [dbo].[mnrFnBI_Fixed]('4E591E71-33BD-4ECC-8703-771BE8A76817') f
where f.BAccCustID != 0x0
order by f.BDate desc,f.BNumber desc,f.biNumber desc
) [k]
Output in SQL Server 2000:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '('.
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'FixbiUnitPrice'.
What's wrong with syntax for SQL Server 2000

Support for 'dynamic' top in sql server started in 2005 version.
for sql server 2000 you can only use a constant number after top.
you can probably use SET ROWCOUNT for your query.
Also, Read this post and it's answers.

In addition to #Zohar's point that a variable TOP #N isn't supported in Sql 2000, what you could also do is generate Dynamic Sql and then execute it, i.e.:
DECLARE #TopCount INT
DECLARE #Sql NVARCHAR(2000)
SELECT #TopCount = COUNT(*)
FROM [dbo].[mnrFnBI_Fixed]('4E591E71-33BD-4ECC-8703-771BE8A76817')
SET #Sql =
'select top 1
[k].[FixbiUnitPrice]
from (
select top ' + CONVERT(NVARCHAR(50), #TopCount) + '
[FixbiUnitPrice],
BDate,
biNumber
From [dbo].[mnrFnBI_Fixed](''4E591E71-33BD-4ECC-8703-771BE8A76817'') f
where f.BAccCustID != 0x0
order by f.BDate desc,f.BNumber desc,f.biNumber desc
) [k]'
EXEC sp_executesql #Sql
That said, Sql 2000 is really an unsupported, legacy technology and you need to consider upgrading to a more recent version of Sql Server as soon as possible.

Related

Passing multiple parameters to variables of sql command in linked server

Here is a working example of how to pass parameters to a SQL statement on a linked server
EXEC ('SELECT ?, ?', 1, 2) AT [CDB]
But this method no works
EXEC ('
SET #A = ?;
SET #B = ?;
SELECT #A, #B', 1, 2) AT [LINKEDSERVER]
and generate error (the linked server is MySQL, the command is runnig in MS SQL as local server):
Msg 7215, Level 17, State 1, Line 7
Could not execute statement on remote server 'LINKEDSERVER'.
Sorry, I cannot attach the text of the message here, as it is in Czech (something like OLE DB error check status values).
However, I necessarily need to pass more parameters to variables without side effect Select.
So how do you do it?
But I don't want to use sp_executesql. In addition, I need to save the result of the execution on the linked server in a temporary table on the local server.
INSERT #Temptable (columns, ...)
EXEC (' more complex query with UNION ', params, ...) AT [LINKEDSERVER]
Jaroslav

Getting a syntax error when declaring a table in SQL

New to SQL and trying to run this code and I get the error "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '#Qtable TABLE(Qid INT) END' at line 3". The code runs fine if I take out the DECLARE statement;
SET #query_id = (SELECT ID from ua820988_dev.users WHERE `email` = 'example#gmail.com');
CREATE PROCEDURE query_requests()
BEGIN
DECLARE #Qtable TABLE(Qid INT)
END;
SELECT * from ua820988_dev.requests WHERE `match` = #query_id;
SELECT * from ua820988_dev.requests_archive WHERE `match` = #query_id;
I'm hoping to eventually put the results from the 2nd and 3rd SELECT statements into the table, but this is the watered down version for now just trying to get the code to run. I'm running SQL 5.6 on MariaDB 10.2.

IF Database not Exists then Truncate command giving error in SQL SERVER 2016 but its working fine in lower version in SQL Server .. please suggest

IF EXISTS (SELECT 1 FROM Master..SysDatabases WHERE [name] = 'abcd')
BEGIN
TRUNCATE TABLE abcd.dbo.ascf
END
As a one of the workarounds:
IF EXISTS (SELECT 1 FROM Master..SysDatabases WHERE [name] = 'abcd')
BEGIN
EXEC ('TRUNCATE TABLE abcd.dbo.ascf')
END
Lowering of compatibility level to SQL 2008 - SQL2014 will not help.

How to write UPDATE SQL with Table alias in SQL Server 2008?

I have a very basic UPDATE SQL -
UPDATE HOLD_TABLE Q SET Q.TITLE = 'TEST' WHERE Q.ID = 101;
This query runs fine in Oracle, Derby, MySQL - but it fails in SQL server 2008
with following error:
"Msg 102, Level 15, State 1, Line 1 Incorrect syntax near 'Q'."
If I remove all occurrences of the alias, "Q" from SQL then it works.
But I need to use the alias.
The syntax for using an alias in an update statement on SQL Server is as follows:
UPDATE Q
SET Q.TITLE = 'TEST'
FROM HOLD_TABLE Q
WHERE Q.ID = 101;
The alias should not be necessary here though.
You can always take the CTE, (Common Tabular Expression), approach.
;WITH updateCTE AS
(
SELECT ID, TITLE
FROM HOLD_TABLE
WHERE ID = 101
)
UPDATE updateCTE
SET TITLE = 'TEST';

C# SQL Top as parameter

Trying to parameterize the value of TOP in my sql statement.
SELECT TOP #topparam * from table1
command.Parameters.Add("#topparam",SqlDbType.VarChar, 10).Value = somevalue.ToString();
This doesn't seem to work. Anyone have any suggestions?
Just to clarify, I don't want to use stored procedures.
In SQL Server 2005 and above, you can do this:
SELECT TOP (#topparam) * from table1
You need to have at least SQL Server 2005. This code works fine in 2005/8 for example ...
DECLARE #iNum INT
SET #iNum = 10
SELECT TOP (#iNum) TableColumnID
FROM TableName
If you have SQL Server 2000, give this a try ...
CREATE PROCEDURE TopNRecords
#intTop INTEGER
AS
SET ROWCOUNT #intTop
SELECT * FROM SomeTable
SET ROWCOUNT 0
GO
You could write an inline query:
EXEC 'SELECT TOP ' + #topparam + ' * FROM ... '
Parse it as an int and that will prevent a SQL injection attack.