How write a query in SQL Server 2000? - sql

I have 2 databases with the same tables and views, one in SQL Server 2008 and another in SQL Server 2000.
I wrote this query and it works in SQL Server 2008, but it didn't work in SQL Server 2000.
How can I change my code to work in SQL Server 2000 ?
SELECT
SUM(NA_DA) OVER (PARTITION BY vd.SI_VoucherH) AS a,
SUM(NA_CA) OVER (PARTITION BY vd.SI_VoucherH) AS b
FROM
acc.ACC_VOUCHERH vh
INNER JOIN
acc.Acc_VoucherD vd ON vh.SI_VoucherH = vd.SI_VoucherH

It's hard to say w/o knowing which table NA_DA & NA_CA are coming from or knowing which table has SI_VoucherH as it PK and which has it a FK.
Hopefully the following will get you close...
SELECT
vda.NA_DA,
vda.NA_CA
FROM
acc.ACC_VOUCHERH vh
JOIN (
select
vd.SI_VoucherH,
NA_DA = Sum(vd.NA_DA),
NA_CA = SUM(NA_CA)
FROM
acc.Acc_VoucherD vd
GROUP BY
vd.SI_VoucherH
) vda
on vh.SI_VoucherH=vda.SI_VoucherH;

Related

How to select "Group" column name from another table in an SQL Server?

Group is column in one of the table.
I am using below query. As Group is reserve keyword, getting error while executing below query:
select o.group as group,
p.id as id
from
product p left join org o on p.id=o.id
Could anyone guide?
Regular SQL Server way:
select o.[group] as [group],
And SQL Server does also support the ANSI SQL way (perhaps some setting needed?)
select o."group" as "group",

Translating MS Access Query for SQL Server

I am trying to recreate a query that was done in MS Access, and now is being handled in a SQL Server environment. I understand that some of the SQL syntax is different in Access than it is in SQL Server. Is there somewhere online that points out the main differences, or will help translate one to the other?
This is a query that I need to update to use in SQL Server:
UPDATE
dbo.TPR00100
INNER JOIN (
dbo.UPR10302
LEFT JOIN dbo.B3980280 ON dbo.TPR10302.COMPTRNM = dbo.B3980280.COMPTRNM
) ON dbo.TPR00100.STAFFID = dbo.TPR10302.STAFFID
SET
dbo.B3980280.COMPTRNM = dbo.TPR10302.comptrnm,
dbo.B3980280.BI_LOC_ID = dbo.TPR00100.locatnid
WHERE
(((dbo.B3980280.COMPTRNM) Is Null))
What are they key aspects that need to be handled differently in a SQL Server transaction for this query?
If find it handy to use an updateable CTE for this:
with cte as (
select
b39.comptrnm b39_comptrnm
b39.bssi_loc_id b39_bssi_loc_id,
tpr.comptrnm tpr_comptrnm,
tpr.locatnid tpr_locatnid
from dbo.tpr00100 tpr
inner join dbo.upr10302 upr on tpr.staffid = upr.staffid
inner join dbo.b3980280 b39 on tpr.comptrnm = b39.comptrnm
where b39_comptrnm is null
)
update cte
set b39_comptrnm = tpr_comptrnm, b39_bssi_loc_id = tpr_locatnid
Note: I am not really sure why the table to update is left joined in the original query, so I turned it to an `inner join .

Query in SQL Server 2000

I have 2 SQL Server databases with same structure, first one in SQL Server 2008 and the other in SQL Server 2000.
I wrote a query in SQL Server 2008 like this :
SELECT
sph.SiProforma, SuProforma, cps.Tp_FamilyOffice_Name,
DsProforma, NaProformaFee, NqCount,
NaProformaFee * NqCount as jameradif,
SUM(NaProformaFee * NqCount) OVER (PARTITION BY suProforma) AS ghabelepardakht
and it works in SQL Server 2008, but when I run it in SQL Server 2000 I get this error :
Incorrect syntax near the keyword 'OVER'.
How can I fix it? Or replace code ?
SQL Server 2000 doesn't support OVER. Write a sub query that performs a SUM(..) GROUP BY suProforma and join the sub query to the main table on suProforma
I'd have written an example for you using your tables but for 2 reasons: one that, I'm posting from an iPhone 4 and reformatting your query (it's a bit of a mess- you will do yourself more favours keeping your code neater) is very very hard work and 2, it's not obvious where suProforma column is kept in which table
Here is a simpler example you can apply to your query:
SELECT
t1.*,
sq1.summed
FROM
table1 t1
INNER JOIN
(SELECT sum(a*b) as summed FROM table1 GROUP BY suProforma) sq2 ON T1.suProforma = sq2.suProforma
To apply this query to your situation replace table1 with the table that has suProforma and correct the SUM(a*b) with your actual columns. Then add your other joins in
If he columns you are grouping by and summing are in different tables, then you will have to write a query that links the two together and paste that in (surrounded by brackets) in place of table1
For example these two things are the same:
SELECT *
FROM table1
SELECT *
FROM (select * from table1) sq1
Get into the habit of looking at any entity in the FROM part of a query as "just a rectangular block of data from which I can select results"
This query will work in both SQL Server 2000 and 2008

Update query on SQL Server with link server with oracle

I want to update column in SQL Server 2008 from link server (oracle) table.
I have table columns are opr_code, m_code etc.
In SQL Server table which has value in opr_code.
I want to update m_code value in SQL Server from link server (oracle) where common value is opr_code which is conf_code in oracle. I tried with following query
update test_S set m_code=A.M_CODE from
(Select * FROM OPENQUERY(linkserver,'Select * From abcd.NAME_desk)) A
inner join test_S B on b.opr_code=a.conf_code
UPDATE t
SET m_code = l.m_code
FROM test_s t
JOIN OPENQUERY(linkserver,'SELECT conf_code, m_code FROM abcd.name_desk') l
ON t.opr_code = l.conf_code

"Incorrect syntax near 'OFFSET'" modift sql comm 2012 to 2008

I'm listing questions with this
SELECT q.qTitle, q.qDescription, q.qCreatedOn, u.uCode, u.uFullname, qcat.qcatTitle, q.qId, q.qStatus
FROM tblQuestion AS q INNER JOIN tblUser AS u
ON q.uId = u.uId INNER JOIN tblQuestionCategory AS qcat
ON q.qcatId = qcat.qcatId
WHERE (q.qStatus = 1)
ORDER BY q.qCreatedOn DESC
OFFSET #page*10 ROWS FETCH NEXT 10 ROWS ONLY
But there is a problem in my server,
Incorrect syntax near 'OFFSET'.
Invalid usage of the option NEXT in the FETCH statement.
How can I modify my query for sql server 2008?
One more question. How can I write a stored procedure for listing pages? Here is my full of code http://codepaste.net/gq5n6c
Answer: http://codepaste.net/jjrkqr
For people using Entity Framework, particulary database first, this error can occur if you develop with SQL 2012 but deploy to an earlier version.
The problem will occur if you use Take...Skip functionality, as SQL 2012 has a new syntax for this. See:
http://erikej.blogspot.co.uk/2014/12/a-breaking-change-in-entity-framework.html
The fix is to edit your .edmx file and change the ProviderManifestToken value from 2012 to your database version, e.g. 2008.
As found out in the comments the reason for the error is because of the fact that SQL Server 2008 does not support it. You may try to change the query according to SQL Server 2012.
Something like this:-
SELECT column1
FROM (
SELECT column1, ROW_NUMBER() OVER (ORDER BY column_id) AS x
FROM mytable
) AS tbl
WHERE tbl.x BETWEEN 20 AND 30
In your code:-
SELECT * FROM
(SELECT ROW_NUMBER() OVER(ORDER BY q.qId) AS rownumber
FROM tblQuestion AS q
INNER JOIN tblUser AS u ON q.uId = u.uId
INNER JOIN tblQuestionCategory AS qcat ON q.qcatId = qcat.qcatId ) as somex
WHERE somex.rownumber BETWEEN 11 AND 20
The issue is because you have not defined #page.
Try this (As you have not mentioned what is #page. I am taking it as some constant or may be you can declare it and then set the value for it):-
declare #page int
set #page = 5 // You may set any value here.
SELECT q.qTitle, q.qDescription, q.qCreatedOn, u.uCode,
u.uFullname, qcat.qcatTitle, q.qId, q.qStatus
FROM tblQuestion AS q
INNER JOIN tblUser AS u ON q.uId = u.uId
INNER JOIN tblQuestionCategory AS qcat ON q.qcatId = qcat.qcatId
WHERE (q.qStatus = 1)
ORDER BY q.qCreatedOn DESC
OFFSET (#page*10) ROWS
FETCH NEXT 10 ROWS ONLY
I encountered this when using Entity Framework. I was developing on a machine with SQL Server 2012. But deployed on a machine with SQL Server 2008. Instead of doing a skip and take on the query, I did a ToList() on the query and did a skip/take on that ToList() in memory. Not ideal, but at least it will work.
When working in a team with multiple versions off Sql Server the model Edmx project
changes the property : ProviderManifestToken.
I solved the problem by changing it back to my Version.