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

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;

Related

SQL Incorrect syntax: CREATE VIEW must be the only statement in the batch

I'm struggling with a problem in SQL - I keep getting this error:
Incorrect syntax: CREATE VIEW must be the only statement in the batch
I'm using newest Microsoft SQL Server Management Studio.
CREATE VIEW [name]
AS
SELECT
Dostawcy.Nazwa_Firmy,
Dostawcy.Szef_Imie,
Dostawcy.Szef_Nazwisko,
Towary.Nazwa,
SUM(Towary_Dostawy.Ilosc) AS Suma
FROM
Dostawcy
INNER JOIN
Dostawy ON Dostawcy.Nazwa_Firmy = Dostawy.Firma_Dostarczajaca
INNER JOIN
Towary_Dostawy ON Dostawy.ID_Dostawy = Towary_Dostawy.ID_Dostawy
INNER JOIN
Towary ON Towary_Dostawy.ID_Towaru = Towary.ID_Towaru
WHERE
Towary_Dostawy.ID_Towaru = 4
GROUP BY
Towary_Dostawy.ID_Towaru, Towary.Nazwa,
Dostawcy.Nazwa_Firmy, Dostawcy.Szef_Imie,
Dostawcy.Szef_Nazwisko
ORDER BY
Suma DESC
SELECT TOP 1 *
FROM name
I checked if the CREATE VIEW was at the beginning of the code, tried something with GO, but maybe wrong.
Add a go to the line above select top 1 * from [name]
Edit: also, the order by should be removed as it has no effect inside of a view.

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.

MS Access ODBC Error 6623: A Winsock virtual circuit was aborted

I tried to create a query with multiple tables in it, all work fine except that when I add this one table, I got ODBC call failed error
6623: A Winsock virtual circuit was aborted.
I used Advantage SQL to link the external database to create reports from it.
The sql for the query that gives the error:
SELECT podetail.ItemPartNbr
,podetail.ItemDescription
,Sum(podetail.Qty) AS LastYearOrdQty
FROM poheader
LEFT JOIN podetail
ON poheader.PoNbr = podetail.PoNbr
WHERE poheader.PoDate >= DateSerial(Year(Date())-1,Month(Date()),1)
and poheader.PoDate <= Date()
GROUP BY podetail.ItemPartNbr
,podetail.ItemDescription;
The main sql where I want to combine other tables with the query above:
SELECT
itemmast.ItemPartNbr
, itemmast.Description
, Sum(iteminv.QtyOnHand) AS SumOfQtyOnHand
, itemmast.MinOrderQty
, itemmast.Cost
, Sum(iteminv.QtyAllocated) AS SumOfQtyAllocated
, itemmast.ReOrderQty
, QtyLastYearPurchase.LastYearOrdQty
FROM
(itemmast
LEFT JOIN iteminv ON itemmast.ItemPartNbr = iteminv.ItemPartNbr)
LEFT JOIN QtyLastYearPurchase ON (itemmast.Description = QtyLastYearPurchase.ItemDescription)
AND (itemmast.ItemPartNbr = QtyLastYearPurchase.ItemPartNbr)
GROUP BY
itemmast.ItemPartNbr
, itemmast.Description
, itemmast.MinOrderQty
, itemmast.Cost
, itemmast.ReOrderQty
, QtyLastYearPurchase.LastYearOrdQty;
I set the joint fields by the ItemPartNbr and I just need the QtyLastYearPurchase.LastYearOrdQty from the first query above to be added into the second query.
I tried to open each one of the queries/tables including the one that gives error and they all open just fine individually, so it doesn't have anything to do with the connection.
If I remove WHERE (((poheader.PoDate)>=DateSerial(Year(Date())-1,Month(Date()),1) And (poheader.PoDate)<=Date())) from the first query, the second query can display the records just fine, but it takes extremely long to show the records.
Any recommendation to fix this? Thank you!
So I tried to create queries between the itemmast and the iteminv tables, and connect the itemmast table with the query i just created and the first query I posted. It works now for now, except that it is still taking forever to open.
This is a bit troublesome since I have to create multiple queries just to combine them all into one query. I have almost 20 queries just to create three reports.

SQL Statement with OR never finishes executing, freezes SQL Database

I have an SQL statement
SELECT dbo.sem_computer.COMPUTER_NAME,dbo.sem_computer.COMPUTER_ID,
[IP_ADDR1_TEXT],dbo.sem_computer.COMPUTER_DOMAIN_NAME, [ID],dbo.SEM_AGENT.AGENT_VERSION
FROM sem_computer, [dbo].[V_SEM_COMPUTER], IDENTITY_MAP, SEM_CLIENT,dbo.SEM_AGENT
WHERE [dbo].[V_SEM_COMPUTER].COMPUTER_ID = SEM_COMPUTER.COMPUTER_ID and dbo.IDENTITY_MAP.ID = dbo.SEM_CLIENT.GROUP_ID
and dbo.SEM_COMPUTER.COMPUTER_ID = dbo.SEM_CLIENT.COMPUTER_ID
AND dbo.SEM_COMPUTER.COMPUTER_ID = dbo.SEM_AGENT.COMPUTER_ID
AND NAME = 'My Company\Default Group'
OR NAME = 'My Company\Bronx'
order by [IP_ADDR1_TEXT]
That executed indefinitely and even freezes the SQL server when I tried to copy and paste this code.
If I remove the
OR NAME = 'My Company\Bronx'
then the the SQL statement executes just fine
We are using SQL Server 2008
Thank you
I think this whole problem becomes a lot clearer if you write ANSI-Compliant T-SQL. So rather than have your able join conditions in the WHERE clause, you have something like:
SELECT
dbo.sem_computer.COMPUTER_NAME
, dbo.sem_computer.COMPUTER_ID
, [IP_ADDR1_TEXT]
, dbo.sem_computer.COMPUTER_DOMAIN_NAME
, [ID]
, dbo.SEM_AGENT.AGENT_VERSION
FROM
sem_computer AS COM
INNER JOIN
[dbo].[V_SEM_COMPUTER] AS V
ON
COM.COMPUTER_ID = V.COMPUTER_ID
INNER JOIN
dbo.SEM_CLIENT AS CLI
ON
COM.COMPUTER_ID = CLI.COMPUTER_ID
INNER JOIN
dbo.SEM_AGENT AS AGT
ON
COM.COMPUTER_ID = AGT.COMPUTER_ID
INNER JOIN
IDENTITY_MAP AS IM
ON
CLI.GROUP_ID = IM.ID
...
Then your WHERE clause does what it is designed to do, which is filter your data. This becomes, as suggested earlier
WHERE
NAME IN('My Company\Default Group','My Company\Bronx')
The performance problem you had was, as pointed out, you were getting a cross join of all tables. But I think you would have noticed this if you write your joins in an ANSI compliant way.
I hope that helps.
Ash
OR NAME IN('My Company\Default Group','My Company\Bronx')
Should work.
When you have the AND NAME = 'My Company\Default Group'
OR NAME = 'My Company\Bronx' not in parentheses or using an IN clause, it means that it will get everything where the OR condition is satisfied, ignoring all of the other conditions.

Resources