Why won't this SQL statement run - sql-server

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

Related

Spotfire: My SQL Queries are not running in Spotfire while it runs in SQL Server Management Studio?

Shows the following error-
External error:
Incorrect syntax near the keyword 'Use'.
Incorrect syntax near ')'.
Submitted query:
Use VPDC
exec vpdc.pa.spMaybeDrop #AllSOText
SELECT DISTINCT dt.UnitID, dt.MainPlatform, dt.SBU, dt.country,dt.HighestContract, pa.fnStripLeadingZeroes(dSO.ServiceOrderNumber) ServiceOrderNumber, dso.StatusOrderNumber, dso.StatusOrderNumberText, ServiceOrderText, ServiceOrderLongText, Cast(BasicStart as date) BasicStart, orderType
into #AllSOText
from aa.vwSAPServiceOrders_OneLinePerServiceOrder dSO
inner join aa.vwTurbines dt on dt.UnitId = dso.UnitId
While it works fine in SQL Server Management Studio.
Remove the "Use VPDC". This is specific to management studio to change context to a database. In client apps this is specified in the connection string or you can write the script with database context, as below:
exec vpdc.pa.spMaybeDrop #AllSOText
SELECT DISTINCT dt.UnitID, dt.MainPlatform, dt.SBU, dt.country,dt.HighestContract, pa.fnStripLeadingZeroes(dSO.ServiceOrderNumber) ServiceOrderNumber, dso.StatusOrderNumber, dso.StatusOrderNumberText, ServiceOrderText, ServiceOrderLongText, Cast(BasicStart as date) BasicStart, orderType
into #AllSOText
from vpdc.aa.vwSAPServiceOrders_OneLinePerServiceOrder dSO
inner join vpdc.aa.vwTurbines dt on dt.UnitId = dso.UnitId

CASE STATEMENT for create view in SQL Server 2008

I have a create view query and I want to check if it does not exist yet, then create view. I tried to create like this:
CASE WHEN IS NOT EXISTS vw_Delays
THEN
VIEW vw_Delays AS
SELECT RD_RfileID_fk_ind, SUM(DATEDIFF(day, RD_Startdate, RD_EndDate)) AS delays FROM dbo.t_RfDelay
GROUP BY RD_RfileID_fk_ind
END
but it returns these errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'CASE'.
Msg 102, Level 15, State 1, Line 6
Incorrect syntax near 'END'.
How to solve this? Please can anyone help me to fix this ?
You need to use this code to check for the view's existence:
IF NOT EXISTS (SELECT * FROM sys.views WHERE Name = N'vw_Delays')
.....
The next obstacle you'll encounter is the fact that the CREATE VIEW statement must be the first of a SQL batch - so you cannot have it right after the existence check.
What I usually do is the opposite:
check if the view does exist
and if so - drop the existing view
then create the view from scratch
and I use this code for this setup:
IF EXISTS (SELECT * FROM sys.views WHERE Name = N'vw_Delays')
DROP VIEW dbo.vw_Delays;
GO
CREATE VIEW dbo.vw_Delays
AS
SELECT
RD_RfileID_fk_ind,
SUM(DATEDIFF(day, RD_Startdate, RD_EndDate)) AS delays
FROM
dbo.t_RfDelay
GROUP BY
RD_RfileID_fk_ind

Invalid object name in SQL Join

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 .*

Open query - Oracle with clause

It is possible to use SQL Server's Open Query, with Oracle With AS clause, in query?
SELECT * FROM OPENQUERY(OMSP,
'
WITH OrderReserve AS
(SELECT cvwarehouseid,
lproductid,
SUM(lqty) lqty
FROM iOrdPrdQtyDate
GROUP BY cvwarehouseid,
lproductid)
SELECT CGPCPRD.DWPRDOID, V_LPRODUCTID_DWBOBJECTOID.DWBOBJECTOID
FROM xxx.CGPCPRD CGPCPRD
...
LEFT JOIN OrderReserve ON (
OrderReserve.cvWarehouseId = CGCCWAREHOUSEPARTY.CVWAREHOUSEID
AND OrderReserve.lProductId = V_LPRODUCTID_DWBOBJECTOID.LPRODUCTID
)
')
Without this (and join which use this With AS) query works. I get this error:
OLE DB provider "OraOLEDB.Oracle" for linked server "OMSP" returned
message "ORA-00942: table or view does not exist". Msg 7321, Level 16,
State 2, Line 1 An error occurred while preparing the query " WITH
OrderReserve AS (SELECT cvwarehouseid, lproductid, SUM(lqty) lqty
FROM iOrdPrdQtyDate GROUP BY cvwarehouseid, lproductid) ...
This passed query works in Oracle.

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.

Resources