Invalid object name in SQL Join - sql-server

I am switching to SQL Server from the visual editor approach I used in MS Access. Here is my first attempt. I am joining two tables and keep getting an invalid object. Where am I going wrong? The error message specifically says:
(560180 row(s) affected)
Msg 208, Level 16, State 1, Line 20
Invalid object name 'BillingTable'.
My query is:
SELECT [Tracking_Number]
,[Package_Key]
,[Manifest_Datetime]
,[Packed_Datetime]
,[Order_Number]
,[WMS_Order_Number]
,[Shipped_Warehouse_Name]
,[Carrier]
,[Service_Name]
,[Zone]
,[Estimated_Weight]
,[Estimated_Cost]
,[Preferred_Warehouse_Name]
,[WMS_Shipping_Method_Name]
FROM [Shipping].[dim].[tbl_Package] as PackageTable
where [Manifest_Datetime] > '1/1/2016'
SELECT [Invoice_Date_Key]
,[Order_Date_Key]
,[Package_Key]
,[Billed_Weight]
,[Billed_Weight_Metric]
,[Package_Quantity]
,[Total_Cost_Dollars]
,[Tax_Cost_Dollars]
,[Total_Cost_Billed_Currency]
FROM [Shipping].[fact].[tbl_Shipping_Billing] as BillingTable
JOIN BillingTable
on PackageTable.Package_Key = BillingTable.Package_Key

Your query should look like this:
Select PackageTable.*, BillingTable.*
From [Shipping].[dim].[tbl_Package] as PackageTable
Inner Join [Shipping].[fact].[tbl_Shipping_Billing] as BillingTable
on PackageTable.Package_Key = BillingTable.Package_Key
where PackageTable.[Manifest_Datetime] > '1/1/2016'
You can call out the specific fields you want from those tables instead of using the .*

Related

SQL Server : conversion error when using INSERT INTO SELECT with JOIN with identical source/destination database

I'm trying to do a migrate some data from an old database to a blank copy of the same database. Both has identical table structures and datatypes. However I am getting a conversion error, yet in my script there is no datatype conversion since both tables are identical. Below is my SQL statement:
INSERT INTO [TEST_NEW].[dbo].[RESULT]
SELECT
'[TEST].[dbo].[RESULT].SAMPNO', '[TEST].[dbo].[RESULT].ACODE',
'[TEST].[dbo].[RESULT].ANALYTE', '[TEST].[dbo].[RESULT].RESULT',
'[TEST].[dbo].[RESULT].RESULT2', '[TEST].[dbo].[RESULT].QUALIFY',
'[TEST].[dbo].[RESULT].MDL', '[TEST].[dbo].[RESULT].VIOLTYPE',
'[TEST].[dbo].[RESULT].VIOLVALUE', '[TEST].[dbo].[RESULT].VIOLDATE',
'[TEST].[dbo].[RESULT].VIOLUSER', '[TEST].[dbo].[RESULT].RLTNUM',
'[TEST].[dbo].[RESULT].RMODDATE', '[TEST].[dbo].[RESULT].RMODUSER',
'[TEST].[dbo].[RESULT].DILFACTOR', '[TEST].[dbo].[RESULT].RAWRESULT',
'[TEST].[dbo].[RESULT].STARTDATE', '[TEST].[dbo].[RESULT].ENDDATE',
'[TEST].[dbo].[RESULT].ANALYST', '[TEST].[dbo].[RESULT].hSec',
'[TEST].[dbo].[RESULT].PQL', '[TEST].[dbo].[RESULT].RAWMDL',
'[TEST].[dbo].[RESULT].RAWPQL', '[TEST].[dbo].[RESULT].RLTUNIT',
'[TEST].[dbo].[RESULT].RLTSOURCE', '[TEST].[dbo].[RESULT].REPLNUM',
'[TEST].[dbo].[RESULT].RESULTPART', '[TEST].[dbo].[RESULT].RESULTCHECKSUM',
'[TEST].[dbo].[RESULT].CHECKSUMVERSION', '[TEST].[dbo].[RESULT].AREF'
FROM
[TEST].[DBO].[RESULT]
INNER JOIN
[TEST].[DBO].[SAMPLE] ON [TEST].[dbo].[RESULT].SAMPNO = [TEST].[DBO].[SAMPLE].SAMPNO
WHERE
[TEST].[dbo].[SAMPLE].SSTATUS IN ('I', 'C')
Error from SQL:
Msg 245, Level 16, State 1, Line 21
Conversion failed when converting the varchar value '[TEST].[dbo].[RESULT].VIOLTYPE' to data type smallint

Query error with Ambiguous column name

When executing the following SQL query on Microsoft SQL Server 2008 :
SELECT
[ACCOUNT]
,[Mailto]
,[Site_addr_1]
,[Site_addr_2]
,[Site_addr_3]
,[State_Class]
,[Map_Facet]
,[Key_Map]
,[Neighborhood_Code]
,[Neighborhood_Group]
,[Econ_Area]
,[Yr_Impr]
,[Acreage]
,[Prior_Total_Market_Value]
,[Land_Value]
,[Improvement_Value]
,[Extra_features_Value]
,[Total_Appraised_Value]
,[Total_Building_Area]
,[Econ_Bld_Class]
,[LAND_USE_CODE]
FROM [Pdata].[dbo].[Real_acct]
LEFT JOIN [Pdata].[dbo].[Land]
ON (Real_acct.ACCOUNT = LAND.ACCOUNT)
I get the following error:
Msg 209, Level 16, State 1, Line 1
Ambiguous column name 'ACCOUNT'.
You are doing a join and it seems that both tables have the column ACCOUNT and sql server doesn't know if you want the column ACCOUNT from Real_acct or Land.
Try this:
SELECT
Real_acct.ACCOUNT -- or Land.ACCOUNT
,Mailto
,Site_addr_1
,Site_addr_2
,Site_addr_3
,State_Class
,Map_Facet
,Key_Map
,Neighborhood_Code
,Neighborhood_Group
,Econ_Area
,Yr_Impr
,Acreage
,Prior_Total_Market_Value
,Land_Value
,Improvement_Value
,Extra_features_Value
,Total_Appraised_Value
,Total_Building_Area
,Econ_Bld_Class
,LAND_USE_CODE
FROM [Pdata].[dbo].[Real_acct]
LEFT JOIN [Pdata].[dbo].[Land]
ON (Real_acct.ACCOUNT = LAND.ACCOUNT)
The very first column... the server doesn't know if you want Real_acct.ACCOUNT or LAND.ACCOUNT
#majidarif is completely correct.
The reason is that both tables have a column named "Account".
You need to specify which table you want the value returned from.
Even if they are the same you need to be specific.

Error in SQL Server updating Image datatype from a linked server

This is a table in a Microft Dynamics 2009 database. Our Test database is missing a bunch of Image data, so I would like to update the table in test with the data in production. I'm using this SQL for this update. When I execute this, I get this error:
Msg 8180, Level 16, State 1, Line 1
Statement(s) could not be prepared.
Msg 306, Level 16, State 2, Line 1
The text, ntext, and image data types cannot be compared or sorted, except when using IS NULL or LIKE operator.
Query:
UPDATE INVENTTABLE
SET
Z_IMAGE = i2.Z_IMAGE,
Z_IMAGEMIMETYPE = i2.Z_IMAGEMIMETYPE
FROM INVENTTABLE i1
JOIN [PRODSQLSERVER].[DAX2009DB].[dbo].INVENTTABLE i2
ON i1.RECID = i2.RECID
WHERE i2.Z_IMAGE IS NOT NULL
I can't see a place where I'm attempting to compare or sort the Image data.
Try changing UPDATE INVENTTABLE to UPDATE i1.

Why won't this SQL statement run

SELECT * FROM agency
INNER JOIN TUser
[agency].[dbo].[Matrix_Branch_ID]=[TUser].[dbo].[client_id]
Microsoft SQL Server Managment Studio gives me:
SQL Server Management Studio gives me:
Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near '.'.
Edit
After fixing the syntax errors with
SELECT * FROM agency
INNER JOIN TUser
ON dbo.agency.Matrix_Branch_ID=dbo.TUser.client_id
SQL Server Management Studio now gives me:
Msg 208, Level 16, State 1, Line 1
Invalid object name 'agency'.
Msg 208, Level 16, State 1, Line 1
Invalid object name 'TUser'.
If you're running this in SSMS - are you in the right database; the one that contains those two tables?
You can see that current database you're in when a query window is active - both in a drop-down on the toolbar, as well as the query window's footer.
You're missing the ON keyword See the <joined table> grammar in FROM (Transact-SQL)
<joined_table> ::= {
<table_source> <join_type> <table_source> ON <search_condition>
| <table_source> CROSS JOIN <table_source>
| left_table_source { CROSS | OUTER } APPLY right_table_source
| [ ( ] <joined_table> [ ) ] }
Also the [dbo] between what I presume is the table and fields names is wrong. See Using Identifiers As Object Names
this should work
SELECT * FROM agency
INNER JOIN TUser
ON [agency].[Matrix_Branch_ID]=[TUser].[client_id]
Are you selecting the correct database? SQL Server defaults to using the master database, which is not the one you probably want.
You probably want to qualify the schema on the table, not on the items you are selecting. For instance:
Use [Database_name]
SELECT * FROM dbo.agency
INNER JOIN dbo.TUser ON agency.Matrix_Branch_ID=TUser.client_id

Create Table As With

Im writing sql query and i have issue which i cannot fix.
I'm trying this:
CREATE TABLE a
AS
WITH cteCandidates (Miejscowosc, Ulica, NrDomu, KodPocztowy)
AS
(
SELECT Miejscowosc, Ulica, NrDomu, KodPocztowy
FROM Gimnazja
INTERSECT
SELECT Miejscowosc, Ulica, NrDomu, KodPocztowy
FROM SzkolyPodstawowe
)
Select
e.Lp as 'Gimnazja',
s.Lp as 'Szkoly Podstawowe'
FROM
Gimnazja AS e
INNER JOIN cteCandidates AS c
ON e.Miejscowosc = c.Miejscowosc AND e.Ulica = c.Ulica AND e.NrDomu = c.NrDomu AND e.KodPocztowy = c.KodPocztowy
INNER JOIN SzkolyPodstawowe s
ON s.Miejscowosc = e.Miejscowosc AND s.Ulica = e.Ulica AND s.NrDomu = e.NrDomu AND s.KodPocztowy = e.KodPocztowy
Order By 'Gimnazja'
And errors which i get:
Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'AS'.
Msg 319, Level 15, State 1, Line 3
Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
And i don't know why. I think that my query is proper but clearly not, and i don't understand why.
Perhaps you want:
WITH cteCandidates (...)
SELECT
e.Lp as 'Gimnazja',
s.Lp as 'Szkoly Podstawowe'
INTO a -- < -- table created here
FROM
Gimnazja AS e
INNER JOIN ...;
CREATE TABLE is used to define a table schema. The WITH keyword is used to define a CTE retrieval of data. These are two completely different operations.
If you need a table within your SQL look at http://msdn.microsoft.com/en-us/library/ms174979.aspx to see the syntax for defining it. You then may populate it with INSERT but that's highly unlikely to be from a CTE ( see below)
Usually with CTEs you don't need to insert the data into a table; in the statement immediately after it you can just use the data retrieved as the table has been temporarily defined and populated for you automatically.
A CTE can be used to define a VIEW - see Create view based on hierarchy / used of CTE for an example
Are you trying to create a view? Then it'd work...

Resources