got unexpected value in TDS response at offset:0 - sql-server

I'm using JDBC Driver Version 1.2.2828.100 and Database Sql Server 2008 R2.
I'm doing search operation by joining three tables containing 1,40,000 rows
SELECT USERID, USERLOGIN, DOMAIN FROM USER (nolock), USER01 (nolock), DOMAIN (nolock) WHERE USERLASTNAME= "aa" AND USERFIRSTNAME = "bb" AND USERDOMAINID = DOMAINID AND USERID = USER01ID ORDER BY USERID
I'm getting the below Error and my application crashes immediately
com.microsoft.sqlserver.jdbc.TDSReader throwInvalidTDS
SEVERE: TDSReader#d5ccfa ( ConnectionID:11 TransactionID:0x0000000000000000) got unexpected value in TDS response at offset:0
What does the TDS reponse at offset:0 refers ?
How to resolve this ?

Okay, TDS is tabular data stream. It is how the client (machine) talks to the database (server). You are using the microsoft jdbc driver.
Here are some things to improve your code and fix the problem.
1 - Please use ANSI 20xx standards.
That means the query will be re-written as the following.
-- Updated using newer syntax
SELECT
U0.USERID,
U0.USERLOGIN,
D0.DOMAIN
FROM
USER as U0 with (nolock) INNER JOIN
USER01 as U1 with (nolock) ON U0.USERID = U1.USER01ID INNER JOIN
DOMAIN as D0 with (nolock) U0.USERDOMAINID = D0.DOMAINID
WHERE
U0.USERLASTNAME= "aa" AND
U0.USERFIRSTNAME = "bb"
ORDER BY
U0.USERID
I no not advocate the (nolock) but that is a whole different battle.
2 - Next, look at this article. The user found out that a type case was causing the issue. Make sure the data types are compatible with JAVA.
http://adrielservice.com/blog/?p=362
I hope this helps.
John

Related

AWS Babelfish co-releated sub query provides in correct results

We are migrating a sql server Database to AWS Aurora Postgres with Babelfish enabled. I connected the Babelfish DB from SSMS through 1433 port. While I ran Babelfish compass report and fixed the un supported functionality. There are few issues which occurred as I was actually modifying the queries/SP's. One such issue is, there is a co-related sub query in an SP and when I created that SP in Babelfish, it was all good but when I execute the SP, it errors out with said error below. I tried replacing that with joins and tried using exists, CTE etc, I am unable to replicate the original results. Any help would be greatly appreciated. Below is the sample query. Any help would be appreciated. Thanks.
SELECT t.POLICY_DOCUMENT_ID,
convert(xml, (select rd.Id as TagId, rd.def_id as RelatedDataDefId, rdd.name_tx as [Name], rd.VALUE_TX as TagCode, rdd.DATA_TYPE_CD as TagDataType, rdd.DOMAIN_CD as TagDomainName
from
TEST_DATA rd
join TEST_DATA_DEF rdd on rdd.relate_class_nm = 'PolicyDocument' and rd.def_id = rdd.id and rdd.ACTIVE_IN = 'Y'
--join #tempPDIDs th on rd.relate_id = th.POLICY_DOCUMENT_ID
where rd.relate_id = t.POLICY_DOCUMENT_ID
for xml PATH('PolicyDocumentTag'), ROOT('PolicyDocumentTags'))) AS POLICY_DOCUMENT_TAGS_XML
from
#tempPDIDs t
join TEST_DOCUMENT pd on pd.id = t.POLICY_DOCUMENT_ID and pd.PURGE_DT is null
left join #tempPDIDFTSTX tmp on pd.id = tmp.POLICY_DOCUMENT_ID
Error:
Msg 33557097, Level 16, State 1, Line 72
missing FROM-clause entry for table "t"

Why Hibernate HSQL Concat is not working for MSSQL?

So, I have Hibernate 5.3.1 in a project which connects to different enginees (MySql, Oracle, PostgreSQL and MS SQL), so I can't use native queries.
Let's say I have 3 records in a table, which all of them have the same datetime, but I need to group them only by date (not time). For example, 2019-12-04;
I execute this query:
SELECT
CONCAT(year(tx.date_), month(tx.date_), day(tx.date_)),
iss.code,
COUNT(tx.id)
FROM
tx_ tx
JOIN
issuer_ iss
ON
tx.id_issuer = iss.id
GROUP BY
CONCAT(year(tx.date_), month(tx.date_), day(tx.date_)), iss.code
But, when I test it connected to SQL SERVER 2017, instead of return 20191204, it's returning 2035. In Oracle and MySQL is working fine.
Anyone has any idea why is this happen? I've tried different ways, like use + instead of CONCAT but the result is the same.
I've also tried to extract them for separate (without concat), and they have been returning correct. The problem is, I need to group them by the complete date.
And just for the record, the field is declared as datetime2 in DDBB
How about simply adding them, instead of using CONCAT.
(year(tx.date_)*10000 + month(tx.date_)*100 + day(tx.date_)*1) AS datenum
Thus, try this:
SELECT
CAST((year(tx.date_)*10000 + month(tx.date_)*100 + day(tx.date_)*1) AS string) AS datenum,
iss.code
FROM tx_ tx
JOIN issuer_ iss
ON tx.id_issuer = iss.id
GROUP BY year(tx.date_), month(tx.date_), day(tx.date_), iss.code
Thanks for the hint Gert Arnold gave me. I just didn't realize that the query was adding like if they were numbers in MSSQL.
Finally, I manage to make it work in the 4 RDBMS casting to string first
SELECT
CONCAT(CAST(year(tx.date_) AS string), CAST(month(tx.date_) AS string), CAST(day(tx.date_) AS string)),
iss.code
FROM
tx_ tx
JOIN
issuer_ iss
ON
tx.id_issuer = iss.id
GROUP BY
CONCAT(year(tx.date_), month(tx.date_), day(tx.date_)), iss.code
I tried also casting to TEXT, but it throws exception in MySQL
Why use concat() to begin with?
Assuming Hibernate takes care of converting the non-standard year(), month() and day() functions, then the following should work on any DBMS
SELECT year(tx.date_), month(tx.date_), day(tx.date_), iss.code
FROM tx_ tx
JOIN issuer_ iss ON tx.id_issuer = iss.id
GROUP BY year(tx.date_), month(tx.date_), day(tx.date_), iss.code

Creating multiple joins

So I need to do a product inventory per warehouse. I'm using ms access as front end and SQL server express as a back end.
The query was working fine when I used ms access as linked back end but when I moved my back end to SQL server express when I run the query, a message box is popping up saying:
ODBC call failed. [Microsoft][Sql Server Native CLient 11.0][Sql Server] The multipart identifier "Dbo.tbl_Warehouse.warehouse_ID" could not be bound.(#4104)
So made a query (qry_Product_Warehouse) where each warehouse contains every product
SELECT tbl_Warehouse.Warehouse_ID, tbl_Product.Product_ID
FROM tbl_Warehouse, tbl_Product;
I also have a union query (Qry_Product_Transactions) where all the product transactions are, including which warehouse it was transacted. Like [Transaction_Type],[Date],[Product],[Location],[quantity]
Then I have a table (tbl_product_Quantity) where if the company will do a monthly stock recounting, they will just enter the [Stock_Recount_Date],[Stock_Recount_Quantity],[product],[warehouse].
The problem is the message box is popping up when I run the query like this (Simplified Version of Joins):
[qry_Product_Warehouse].[warehouse_ID] left join [qry_Product_Transactions].[Location]
[qry_Product_Warehouse].[warehouse_ID] left join [tbl_Product_Quantity].[warehouse]
[qry_Product_Warehouse].[product_ID] left join [qry_Product_Transactions].[Product]
[qry_Product_Warehouse].[Product_ID] left join [tbl_Product_Quantity].[Product]
Actual Query is this:
SELECT qry_Product_Warehouse.Warehouse_ID,
qry_Product_Warehouse.Product_ID,
CDbl(Nz(IIf(IsNull([tbl_Product_Quantity].[Stock_Recount_Date]),
Sum([qry_Product_Transaction].[qty]),[tbl_Product_Quantity].[Stock_Recount_Quantity]+Sum(IIf([qry_Product_Transaction].[Date]>[tbl_product_quantity].[Stock_Recount_Date],[qry_Product_Transaction].[Qty],0))),0)) AS Current_Qty
FROM (qry_Product_Warehouse LEFT JOIN qry_Product_Transaction ON (qry_Product_Warehouse.Warehouse_ID = qry_Product_Transaction.Location)
AND (qry_Product_Warehouse.Product_ID = qry_Product_Transaction.Product))
LEFT JOIN tbl_Product_Quantity ON (qry_Product_Warehouse.Product_ID = tbl_Product_Quantity.Product)
AND (qry_Product_Warehouse.Warehouse_ID = tbl_Product_Quantity.Warehouse)
GROUP BY qry_Product_Warehouse.Warehouse_ID, qry_Product_Warehouse.Product_ID, tbl_Product_Quantity.Stock_Recount_Quantity, tbl_Product_Quantity.Stock_Recount_Date;

Joins and linked servers query causing delay

Hey guys the code below is taking a really long time. I've been looking at it for quite a while. Is there anything that stands out as the obvious cause of delay?
[SQLSRV-3-JB] is a linked server BTW
Select count(cast (Unique_ID as bigint)) as [Count],
T.[Region_Code],
T.[Region_Name],
U.[Region_Code],
Y.[Examination_Year],
case when [Subject] = 'MUSIC THEORY' THEN 'Theory'
else 'Practical'
end
from [SQLSRV-3-JB].[X].[dbo].[Exam_and_Candidate_Details] Y
left join [SQLSRV-3-JB].[X].[dbo].[UK_Exam_Centre_Info] T
on Y.Centre_Code = T.Centre_Code
left join [SQLSRV-3-JB].[X].[dbo].[UK_Exam_Centres] U
on Y.Centre_Code = U.Centre_Code
where Y.[Examination_Year] between 2010 and 2016
group by Y.[Examination_Year],
T.[Region_Code],
T.[Region_Name],
U.[Region_Code],
case when [Subject] = 'MUSIC THEORY' THEN 'Theory'
else 'Practical'
end
Yes, there is one very obvious problem - the remote server. When you use a linked server like this, SQL Server has a disturbing habit of pulling all of the data in the remote tables to the local server before performing JOINs or filtration.
The correct way to handle this is to make this query a view on the remote server, and query the view with your WHERE clause on your end. So, your remote code would look like this:
-- Remote Server
USE X
GO
CREATE VIEW dbo.ExamCenterInfo
AS
Select count(cast (Unique_ID as bigint)) as [Count],
T.[Region_Code],
T.[Region_Name],
U.[Region_Code],
Y.[Examination_Year],
case when [Subject] = 'MUSIC THEORY' THEN 'Theory'
else 'Practical'
end
from dbo.[Exam_and_Candidate_Details] Y
left join dbo.[UK_Exam_Centre_Info] T
on Y.Centre_Code = T.Centre_Code
left join dbo.[UK_Exam_Centres] U
on Y.Centre_Code = U.Centre_Code
group by Y.[Examination_Year],
T.[Region_Code],
T.[Region_Name],
U.[Region_Code],
case when [Subject] = 'MUSIC THEORY' THEN 'Theory'
else 'Practical'
end
Then your local code:
-- Local Server
SELECT *
FROM [SQLSRV-3-JB].[X].[dbo].ExamCenterInfo ECI
where ECI.[Examination_Year] between 2010 and 2016
NOTE: There is some possibility that this will pull all of the view output before filtering by year. If this is still a problem, you will have to create a more complex mechanism to execute the view with filtration on the remote server.

How can I get an accurate count of computers logged in to my SQL Server database?

We license our software by number of workstations.
I have a query that I have used for years to get an accurate count of the workstations logged in to my SQL Server database. For simplicity, all users use the same login name/password. This is built in to the script that attaches to the DB. They have access only to that DB with the exception of
USE [Master] GRANT VIEW SERVER STATE to MyUser
The query that has been working is below:
SELECT COUNT(Users) AS UserCount FROM (SELECT COUNT(Master.dbo.sysprocesses.hostname) AS Users FROM Master.dbo.sysprocesses LEFT OUTER JOIN Master.dbo.sysdatabases ON Master.dbo.sysdatabases.dbid = Master.dbo.sysprocesses.dbid WHERE (Master.dbo.sysdatabases.name = 'MyDatabase') GROUP BY Master.dbo.sysprocesses.net_address) AS UserCount_1
Basically this relies on the mac address (Master.dbo.sysprocesses.net_address), since both Workstation names and ip addresses can be duplicated.
Recently this has stopped working at a number of customers. Suddenly individual workstations are showing multiple net addresses for the same workstation causing a substantial overcount of users. This may be related to SQL Server 2012 - not sure.
What I need is a very reliable way to get a count of workstations logged in to my database.
If anyone can tell me why I am suddenly getting multiple net_addresses for each workstation and how to prevent that that would be one possible solution.
Otherwise if anyone can give me a rock solid way to get a workstation count other than the above that would be great. Our largest customer is 50 users by the way.
HERE IS AN EXAMPLE:
SELECT Master.dbo.sysprocesses.hostname AS Users, Master.dbo.sysprocesses.net_address FROM Master.dbo.sysprocesses
LEFT OUTER JOIN Master.dbo.sysdatabases ON Master.dbo.sysdatabases.dbid = Master.dbo.sysprocesses.dbid
WHERE Master.dbo.sysdatabases.name = 'mydb' GROUP BY Master.dbo.sysprocesses.hostname, Master.dbo.sysprocesses.net_address
RETURNS:
DAVID-PC 001CC490239E
FLOOR1 001CC41D8012
FLOOR2 CB8FEE6C5856
FLOOR3 A50B18FF1516
KER-PC7 6C626DEA68CC
LIZ-PC A4E553460E35
LIZ-PC EFE3F0E20260
LIZ-PC FD32F7B30360
PAP 9D35A704C29C
PAP CFB724BA1183
PAP D1A58A8878E6
PAP E9B116CA34B8
PAP F38B335A7AE6
Thanks in advance for any help.
You can get an accurate count of the users logged in to SQL Server database by the following query:
SELECT
DB_NAME(dbid) as DB
COUNT(dbid) as Numb
loginame as LoginNa
FROM
sys.sysprocesses
WHERE
dbid > 0
GROUP BY
dbid, loginame
Also you can get full details of connected users by this:
sp_who2 'Active'
Here is a way that will give you the number of IPs or MAC addresses connected to a given database. I'd go with IP as you might have multiple NICs/MACs in a given high end workstation.
SELECT COUNT(DISTINCT c.client_net_address) as DistinctIPCount,
COUNT(DISTINCT p.net_address) as DistinctMACCount
FROM sys.dm_exec_connections c INNER JOIN sys.dm_exec_sessions s ON c.session_id = s.session_id
INNER JOIN sysprocesses p ON s.session_id = p.spid
WHERE s.is_user_process = 1
AND p.dbid = DB_ID('yourdbnamehere')

Resources