Creating multiple joins - sql-server

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;

Related

ODBC call failed. The multipart identifier "(tablename)" could not be bound. (#4104)

This query doesnt work when i run this query with tables from sql server as linked back end. But works just fine when tables are from linked ms access back end.
I think it has something to do with the join clause.
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;

Recently created index in SQL Server

How to find recently created index details in my SQL Server database? Any query to find this?
In my database there are a lot of indexes. I want to know which of those indexes were recently created, with all their details.
You can use SCHEMA changes history to know index creation changes along with many changes
Below is how you do it..
1.Right click server
2.Goto reports -->standard reports-->Schema changes history
below is screenshot from mt device
Default trace is enabled by default,unless you turn it on
below query tells you,if default trace status is ON
select * from sys.configurations where name like '%trace%'
below query can provide object creation stats
SELECT OBJECT_NAME(objectid),objectname,indexid
FROM sys.fn_trace_gettable(CONVERT(VARCHAR(150), ( SELECT TOP 1
f.[value]
FROM sys.fn_trace_getinfo(NULL) f
WHERE f.property = 2
)), DEFAULT) T
JOIN sys.trace_events TE ON T.EventClass = TE.trace_event_id
where DatabaseName=db_name()
ORDER BY t.StartTime ;

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 to find replication lag in SQL Server?

In MYSQL server by looking into the value for "second behind master" it can be known by how much a slave server is lagging behind to its master. So, is there something similar to it in MSSQL so that it can be known how a slave server is lagging behind by its master?
There is some controversy on this subject, but I like to use regularly posted tracer tokens. That is, you call the sp_posttracertoken procedure on the publisher and it will send a, well, token all the way through to the subscriber. You can see the history of all tokens in the distributor database. I wrote the following view to make the data a little easier to grok:
create view [dbo].[tokens] as
select
ps.name as [publisher],
p.publisher_db,
p.publication,
ss.name as [subscriber],
da.subscriber_db,
t.publisher_commit,
t.distributor_commit,
h.subscriber_commit,
datediff(second, t.publisher_commit, t.distributor_commit) as [pub to dist (s)],
datediff(second, t.distributor_commit ,h.subscriber_commit) as [dist to sub (s)],
datediff(second, t.publisher_commit, h.subscriber_commit) as [total latency (s)]
from mstracer_tokens t
inner join MStracer_history h
on t.tracer_id = h.parent_tracer_id
inner join mspublications p
on p.publication_id = t.publication_id
inner join sys.servers ps
on p.publisher_id = ps.server_id
inner join msdistribution_agents da
on h.agent_id = da.id
inner join sys.servers ss
on da.subscriber_id = ss.server_id
Another approach is to use what's commonly called a canary table. The idea is that you have a table specifically to monitor replication that typically only has one row with a datetime field. You update the column at the publisher and then you monitor how far behind the subscriber is by seeing what the value of that column is at the subscriber.
Lastly, there are some perfmon counters that you can look at. In my experience, they're not that great; the number of outstanding commands is an accurate number, but the measurement of latency as a duration is typically very inaccurate.

got unexpected value in TDS response at offset:0

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

Resources