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.
Related
I have SQL code to call my table in my Laravel controller like this:
elseif(auth()->user()->group_id){
$media = MediaOrder::memberOf(auth()->user()->group_id)
->join('users','users.nik','=','media_order.created_by')
->select('media_order.*','users.nickname')
->get();
But when I tried it it come up with an ambiguous column error, like this:
LOG.error: SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL
Server][SQL Server]Ambiguous column name 'group_id'. (SQL: select
[media_order].*, [users].[nickname] from [media_order] inner join
[users] on [users].[nik] = [media_order].[created_by] where [group_id]
= 6) {"userId":"101","exception":{"errorInfo":["42000",209,"[Microsoft][ODBC
Driver 11 for SQL Server][SQL Server]Ambiguous column name
'group_id'."]}
Does anyone has an solution to this? For joining between tables is working, but I just don't know how to use where to get the table based on group_id.
Thank you!.
It seems like both the media_order and users tables have a group_id column, so perhaps something like the following will allow you to distiguish between them...
$media = DB::table('media_order')
->join('users','users.nik','=','media_order.created_by')
->select('media_order.*','users.nickname')
->where('media_order.group_id', '=', auth()->user()->group_id)
->get();
I have tried creating a view with the help of schema binding and indexing which is referring from other server table. But sql thrown some error for the below query .
create VIEW [dbo].[Vxyz]
with schemabinding
AS
SELECT
ELID,USECOUNT,LASTUPDATE,TYPE,CODENE,CASNUE,NAME_ENG,ISGROUP,CHGROUP,DLink
IDE,LOCKBY,PhyApB,BUILDNO,PMNNumE,EINECE
FROM IADL.dbo.tblxyz
GO
create unique clustered index IDX_xyz on [dbo].
[Vxyz](ELID)
Found below error
Msg 4512, Level 16, State 3, Procedure IADL.dbo.tblxyz, Line 3 [Batch Start Line 11]
Cannot schema bind view '[dbo].[Vxyz]' because name 'IADL.dbo.tblxyz' is invalid for schema binding. Names must be in two-part format
and an object cannot reference itself.
Msg 1939, Level 16, State 1, Line 17
Cannot create index on view '[dbo].[Vxyz]' because the view is not schema bound.
select distinct
ISNULL(A.elid, B.elid) ElementID,
CASE when A.elid is null and B.elid is not null then 'Missing ElementID :'+
B.elid+' in Mainproductsall table' when A.elid is not null
and B.elid is null then 'Missing ElementID :'+ A.elid+' in Genproductsall table' Else 'OK'
end Datastatus
into ABC
from [dbo].[Vxyz] As A
full outer join [dbo].[Vxyzwa] as B on A.elid = B.elid
where A.elid is null or B.elid is null
Each from from above query is view . As per my first query above which is referring from other server. so i want to optimize and i am trying to create index.
If you check official documentation, you will see that it is stated as follows
All referenced objects must be in the same database.
So you cannot refer a base table from an other database.
This means, for all referenced current database objects should be referenced with their schema name and object name.
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 .*
I am very new to Microsoft SQL Server and am using 2012 Management Studio. I get the error above when I try to export query results to an excel file using the wizard. I have seen solutions posted elsewhere for this error but do not know enough to figure out how to implement the solutions recommended. Can somebody please walk me through one of these solutions step by step?
I believe my problem is that the SQL Server Import and Export Wizard Does Not Recognise Varchar and NVarchar which I believe is the data type for the columns that I am receiving errors for.
Source Type 200 in SQL Server Import and Export Wizard?
http://connect.microsoft.com/SQLServer/feedback/details/775897/sql-server-import-and-export-wizard-does-not-recognise-varchar-and-nvarchar#
Query:
SELECT licenseEntitlement.entID, licenseEntitlement.entStartDate, entEndDate, quote.quoteId, quote.accountId, quote.clientId, quote.clientName, quote.contactName,
quote.contactEmail, quote.extReference, quote.purchaseOrderNumber, quote.linkedTicket
FROM licenseEntitlement INNER JOIN
quote ON quote.quoteId = SUBSTRING(licenseEntitlement.entComments, 12, PATINDEX('% Created%', licenseEntitlement.entComments) - 12)
inner join sophos521.dbo.computersanddeletedcomputers on computersanddeletedcomputers.name = entid and IsNumeric(computersanddeletedcomputers.name) = 1
WHERE (licenseEntitlement.entType = 'AVS') AND (licenseEntitlement.entComments LIKE 'OV Order + %') and entenddate < '4/1/2014'
ORDER BY licenseEntitlement.entEndDate
Error:
TITLE: SQL Server Import and Export Wizard
------------------------------
Column information for the source and the destination data could not be retrieved, or the data types of source columns were not mapped correctly to those available on the destination provider.
[Query] -> `Query`:
- Column "accountId": Source data type "200" was not found in the data type mapping file.
- Column "clientId": Source data type "200" was not found in the data type mapping file.
- Column "clientName": Source data type "200" was not found in the data type mapping file.
- Column "contactName": Source data type "200" was not found in the data type mapping file.
- Column "contactEmail": Source data type "200" was not found in the data type mapping file.
- Column "extReference": Source data type "200" was not found in the data type mapping file.
- Column "purchaseOrderNumber": Source data type "200" was not found in the data type mapping file.
- Column "linkedTicket": Source data type "200" was not found in the data type mapping file.
If any more details are needed please let me know
So, implementing the suggestion at the StackOverflow link you gave, of turning the query into a View, here's an example of what that could look like (with some code formatting ;) --
CREATE VIEW [dbo].[test__View_1]
AS
SELECT LIC.entID, LIC.entStartDate, entEndDate,
quote.quoteId, quote.accountId, quote.clientId, quote.clientName,
quote.contactName, quote.contactEmail, quote.extReference,
quote.purchaseOrderNumber, quote.linkedTicket
FROM [dbo].licenseEntitlement LIC WITH(NOLOCK)
INNER JOIN [dbo].quote WITH(NOLOCK)
ON quote.quoteId = SUBSTRING(LIC.entComments, 12,
PATINDEX('% Created%', LIC.entComments) - 12)
INNER JOIN sophos521.dbo.computersanddeletedcomputers COMPS WITH(NOLOCK)
ON COMPS.name = entid and IsNumeric(COMPS.name) = 1
WHERE (LIC.entType = 'AVS')
AND (LIC.entComments LIKE 'OV Order + %')
and (entenddate < '4/1/2014')
ORDER BY LIC.entEndDate
GO
Then, you would export from test__View_1 (or whatever real name you choose for it), as if test__View_1 was the table name.
FYI, after the first time you've executed the above -- after you've "created" the view -- then from then on, the view's first line (during modifications) changes, from CREATE VIEW, to ALTER VIEW.
((And, aside from the bug question... in your WHERE clause, did you intend entComments LIKE 'OV Order + %', or was that really intended to be entComments LIKE 'OV Order%'? I've made that change, in the alternative example code, below.))
Note: if you're going to be exporting repeatedly (or re-using) the output from one run, and especially if your query is slow or hogs the machine... then instead of a VIEW, you might prefer a SELECT INTO, to create a table once, which can be quickly re-used. (I would also choose SELECT INTO rather than CREATE VIEW, when developing a one-time-only query for export.)
IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'zz_LIC_ENT_DETAIL')
DROP TABLE [dbo].zz_LIC_ENT_DETAIL
SELECT LIC.entID, LIC.entStartDate, LIC.entEndDate,
quote.quoteId, quote.accountId, quote.clientId, quote.clientName,
quote.contactName, quote.contactEmail, quote.extReference,
quote.purchaseOrderNumber, quote.linkedTicket
INTO [dbo].zz_LIC_ENT_DETAIL
FROM [dbo].licenseEntitlement LIC WITH(NOLOCK)
INNER JOIN [dbo].quote WITH(NOLOCK)
ON quote.quoteId = SUBSTRING(LIC.entComments, 12,
PATINDEX('% Created%', LIC.entComments) - 12)
INNER JOIN sophos521.dbo.computersanddeletedcomputers COMPS WITH(NOLOCK)
ON COMPS.name = LIC.entid and IsNumeric(COMPS.name) = 1
WHERE (LIC.entType = 'AVS')
AND (LIC.entComments LIKE 'OV Order%')
and (LIC.entenddate < '4/1/2014')
ORDER BY LIC.entEndDate
Then, you would of course export from table zz_LIC_ENT_DETAIL (or whatever table name you chose).
Hope that helps...
It might be easier to right click query results window and choosing Save Results As (CSV)..
To append the column names in the first row you'd also need to modify your query in this way (note the cast for int or datetime columns):
select 'col1', 'col2', 'col3'
union all
select cast(id as varchar(10)), name, cast(someinfo as varchar(28))
from Question1355876
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