This sql statement is not a query Import Wizard - sql-server-2012

I am trying to import data from an excel file to one of my table so I am using the SQL server import and export wizard in the management studio and providing a source query. When I click parse after entering below query, I get This sql statement is not a query.
select
u.UserID, m.IMManagementPlatformID, s.Username
from
([Sheet1$] s
inner join
dbo.wmcTBLUsers u on u.Firstname = s.Firstname
and u.Lastname = s.Lastname)
inner join
IMManagementPlatform m on m.IMManagementPlatformName = s.IMManagementPlatformName
where
u.IsActive = 1
and u.Firstname <> 'First'
and u.Lastname <> 'Last'

This error is from Microsoft Office Access Database Engine.
It parses the query that accesses only Excel file, not SQL Server database,
so you can use only other Excel sheets in your query

Related

Accessing Data in a SQL Query

tell me how to make a user access to data access,
There is a table with dialogs, and you need to build a query so that another user cannot see this data if he does not have access to this dialog.
I tried to do it but it didn't work
SELECT *
FROM NameDS INNER JOIN
DialogueListDS ON NameDS.UserID = DialogueListDS.UserID INNER JOIN
MessegesListDS ON DialogueListDS.DialogueID = MessegesListDS.DialogueID
WHERE NameDS.UserID = '2B3F7596-DEC9-4CAF-A867-0127186CFC5C'

SQL Query error ' multi-part identifier vSMS_CombinedDeviceResources.MachineID could not be bound'

I have to create a custom report in System Center Configuration Manager using SQL Server Report Builder.
I have already selected the dataset and the connection to the SQL database is successfully established. I have to write a Query in SQL to fetch the data from the views. This is the SQL Query:
SELECT
(
vSMS_CombinedDeviceResources.MachineID,
vSMS_CombinedDeviceResources.Name,
vSMS_CombinedDeviceResource.CurrentLogonUser
FROM vSMS_CombinedDeviceResources
INNER JOIN vSMS_Update_ComplianceStatus
ON vSMS_CombinedDeviceResource.MachineID = vSMS_Update_ComplianceStatus.MachineID
INNER JOIN v_UpdateDeploymentSummary
ON v_UpdateDeploymentSummary.CI_ID = vSMS_Update_ComplianceStatus.CI_ID
WHERE v_UpdateDeploymentSummary.CollectionName=#CollectionName
AND vSMS_CombinedDeviceResources.ClientState!=0
)
But I am getting an error as
The multi-part identifier vSMS_CombinedDeviceResources.MachineID
could not be bound
Could anyone please specify the error that I have made in the SQL query?
you have a wrong ( ) after the select word and around the rest of query
SELECT vSMS_CombinedDeviceResources.MachineID
,vSMS_CombinedDeviceResources.Name
,vSMS_CombinedDeviceResource.CurrentLogonUser
FROM vSMS_CombinedDeviceResources
INNER JOIN vSMS_Update_ComplianceStatus ON vSMS_CombinedDeviceResource.MachineID = vSMS_Update_ComplianceStatus.MachineID
INNER JOIN v_UpdateDeploymentSummary ON v_UpdateDeploymentSummary.CI_ID = vSMS_Update_ComplianceStatus.CI_ID
WHERE v_UpdateDeploymentSummary.CollectionName=#CollectionName
AND vSMS_CombinedDeviceResources.ClientState!=0

Access 2010 Query Syntax error

(1) The Access Database is being used as way to connect to 2 Teradata databases
(2) Currently it has 6 linked tables to 2 Teradata databases
(3) It doesn't matter to me if it is an Access Query or a pass-through query to Teradata
(4) The goal is to put something in an Excel macro that will query the 2 Teradata bases and return some information that will be included in a macro
I'm trying to create a SQL query in Access with SQL that works in Teradata. When I try to run the query, I get am error message (Syntax error in FROM clause) and the 1st Join is highlighted. I've written queries in Access before but nothing this complex. We are using Access 2010. Any suggestions on the syntax error would be greatly appreciated. Thanks for the help.......
select veh_mgmt_csr.cst_bo_item.veh_lgcy_nbr as unit, veh_mgmt_csr.cst_bo_item.veh_odmtr_qty as mileage, veh_mgmt_csr.rm_cust_mast.upp_lgl_cust_nam as cust_legal_name, veh_mgmt_csr_rv.cst_address.addr_st_nam as address,
veh_mgmt_csr_rv.cst_address.addr_cty_nam as city, veh_mgmt_csr_rv.cst_address.addr_postl_cde as zip, veh_mgmt_csr_rv.cst_address.stprov_cde as state, veh_mgmt_csr_rv.cst_address.cntry_iso_cde as country, veh_mgmt_csr_rv.cst_address.addr_phn_nbr as location_phone, e.contact_name, e.contact_phone
from veh_mgmt_csr.cst_buyer_order
Join veh_mgmt_csr.cst_bo_item on veh_mgmt_csr.cst_buyer_order.bo_id = veh_mgmt_csr.cst_bo_item.bo_id and veh_mgmt_csr.cst_bo_item.veh_invy_stat_dsc = 'SOLD'
Join veh_mgmt_csr.rm_cust_mast on veh_mgmt_csr.cst_buyer_order.rm_cust_id = veh_mgmt_csr.rm_cust_mast.rm_cust_id
Join VEH_MGMT_CSR_RV.cst_address on veh_mgmt_csr.rm_cust_mast.prim_addr_id = veh_mgmt_csr_rv.cst_address.addr_id and veh_mgmt_csr_rv.cst_address.record_status = 'A'
left join (select veh_mgmt_csr.cst_buyer_order.e_o_id cust_nbr,trim(veh_mgmt_csr.cst_individual.indiv_upp_frst_nam) || ' ' || trim(veh_mgmt_csr.cst_individual.indiv_upp_last_nam) contact_name,
VEH_MGMT_CSR_RV.CST_PHONE_NBR.phn_nbr contact_phone from veh_mgmt_csr.cst_e_o_cntct
left join veh_mgmt_csr.cst_individual on veh_mgmt_csr.cst_e_o_cntct.indiv_id = veh_mgmt_csr.cst_individual.indiv_id
left join VEH_MGMT_CSR_RV.CST_PHONE_NBR on veh_mgmt_csr.cst_e_o_cntct.indiv_id = VEH_MGMT_CSR_RV.CST_PHONE_NBR.indiv_id and VEH_MGMT_CSR_RV.CST_PHONE_NBR.prim_phn_ind = 1
qualify rank() over (partition by veh_mgmt_csr.cst_e_o_cntct.e_o_id order by contact_name asc) = 1) e on veh_mgmt_csr.rm_cust_mast.rm_cust_id = e.cust_nbr
where veh_mgmt_csr.cst_individual.veh_lgcy_nbr = '8B5RG1'
and veh_mgmt_csr.cst_e_o_cntct.cntry_iso_cde in ('US','CA')
and extract(year from veh_mgmt_csr.cst_e_o_cntct.bo_dte) = 2017
As a beginner to SQL, please note that while most RDBMS's including MS Access and Teradata may run ANSI-SQL (basic, standard DDL/DML statements), almost no two RDBMS's share the same dialects (ANSI-plus). Each maintains their own styles and specific methods.
Additionally, do note MS Access comes in two folds: 1) a GUI .exe application and 2) a database engine (ACE/JET database engine). Over time it has been conflated to be the same but they are not. See this meta post. The former .exe application by default connects to the default database engine but this default can be switched out for other backends like Teradata.
However, the method of connection between frontend GUI and backend database (i.e., linked tables, pass-through queries, application code) will differ in the SQL dialect used.
linked tables => MS Access SQL dialect
pass-through queries => Backend RDBMS database dialect
application code (i.e., VBA) => Backend RDBMS database dialect
You may be attempting to run Teradata SQL on MS Access linked tables, hence violating #1 with resulting syntax errors. As seen below with proper indentation, there are several incompatible syntaxes in your attempted query:
MS Access uses only one period qualifier between table name and column name. Likely you may be referencing a named schema, only available in Teradata.
MS Access does not use JOIN by itself but requires INNER, LEFT, or RIGHT (no OUTER);
MS Access requires parentheses whenever pairs of tables are used in JOIN clauses;
MS Access does not support window functions such as RANK() OVER...;
MS Access uses & in concatenation not double pipes || and uses Year() instead of extract(year ...); and does not use qualify, possibly strictly a Teradata method;
MS Access requires AS for column aliases such as for contact_name and contact_phone.
SQL
SELECT veh_mgmt_csr.cst_bo_item.veh_lgcy_nbr AS unit,
veh_mgmt_csr.cst_bo_item.veh_odmtr_qty AS mileage,
veh_mgmt_csr.rm_cust_mast.upp_lgl_cust_nam AS cust_legal_name,
veh_mgmt_csr_rv.cst_address.addr_st_nam AS address,
veh_mgmt_csr_rv.cst_address.addr_cty_nam AS city,
veh_mgmt_csr_rv.cst_address.addr_postl_cde AS zip,
veh_mgmt_csr_rv.cst_address.stprov_cde AS state,
veh_mgmt_csr_rv.cst_address.cntry_iso_cde AS country,
veh_mgmt_csr_rv.cst_address.addr_phn_nbr AS location_phone,
e.contact_name,
e.contact_phone
FROM veh_mgmt_csr.cst_buyer_order
JOIN veh_mgmt_csr.cst_bo_item
ON veh_mgmt_csr.cst_buyer_order.bo_id = veh_mgmt_csr.cst_bo_item.bo_id
AND veh_mgmt_csr.cst_bo_item.veh_invy_stat_dsc = 'SOLD'
JOIN veh_mgmt_csr.rm_cust_mast
ON veh_mgmt_csr.cst_buyer_order.rm_cust_id = veh_mgmt_csr.rm_cust_mast.rm_cust_id
JOIN veh_mgmt_csr_rv.cst_address
ON veh_mgmt_csr.rm_cust_mast.prim_addr_id = veh_mgmt_csr_rv.cst_address.addr_id
AND veh_mgmt_csr_rv.cst_address.record_status = 'A'
LEFT JOIN
(
SELECT veh_mgmt_csr.cst_buyer_order.e_o_id cust_nbr,
Trim(veh_mgmt_csr.cst_individual.indiv_upp_frst_nam)
|| ' ' ||
Trim(veh_mgmt_csr.cst_individual.indiv_upp_last_nam) contact_name,
veh_mgmt_csr_rv.cst_phone_nbr.phn_nbr contact_phone
FROM veh_mgmt_csr.cst_e_o_cntct
LEFT JOIN veh_mgmt_csr.cst_individual
ON veh_mgmt_csr.cst_e_o_cntct.indiv_id = veh_mgmt_csr.cst_individual.indiv_id
LEFT JOIN veh_mgmt_csr_rv.cst_phone_nbr
ON veh_mgmt_csr.cst_e_o_cntct.indiv_id = veh_mgmt_csr_rv.cst_phone_nbr.indiv_id
AND veh_mgmt_csr_rv.cst_phone_nbr.prim_phn_ind = 1
qualify rank() OVER (partition BY veh_mgmt_csr.cst_e_o_cntct.e_o_id
ORDER BY contact_name ASC) = 1) e
ON veh_mgmt_csr.rm_cust_mast.rm_cust_id = e.cust_nbr
WHERE veh_mgmt_csr.cst_individual.veh_lgcy_nbr = '8B5RG1'
AND veh_mgmt_csr.cst_e_o_cntct.cntry_iso_cde IN ('US','CA')
AND extract(year FROM veh_mgmt_csr.cst_e_o_cntct.bo_dte) = 2017;
Per your update do the following:
To keep above Teradata syntax, create 2 pass-through queries (using same ODBC connection as linked tables) for each database.
Run same make-table query (SELECT * INTO myAccessTable FROM myTeradataPassThroughQuery) to move Teradata results into Access local tables.
Join the two new tables for your final end result using Access SQL syntax.

Crystal Report Multiple data sources - Table Not Found

Using Crystal Reports version 13.0 in Visual Studio 2010.
Using the Command feature, all the fields needed are selected.
One of the fields is from a separate database on the same server.
SELECT r.ID, r.Revision,
u.FirstName + ' ' + u.LastName AS UpdateBy, r.UpdateDate,
s.Description AS Status, r.StatusID, r.Comments,
c.Notes
FROM Repel r
INNER JOIN Status s ON r.StatusID = s.id
INNER JOIN Conditions c ON c.SOCID = r.ID
LEFT OUTER JOIN [Admin].[dbo].[Users] u ON u.ID = r.UpdateBy
WHERE r.ID = {?item}
Using the database expert, both databases are connected but the command itself is on the main datasource.
The datasource with the tables Repel, Status, and Conditions.
It can NOT find the table Users.
The path is fully qualified so how can I get the report to see this table?
I know I could create a view in sql server itself and connect to that but there is a change that needs to be made to 30+ reports.
Thanks!!
EDIT
I tried to create a view in the main database only to use one datasource.
Then changed the line...
LEFT OUTER JOIN vUsers u ON u.ID = r.UpdateBy
Now I get the error:
This field name is not known.
Final EDIT
Doing more testing, I removed all database connections and fields from the report.
The unknown field name still appeared.
So, I removed everything from the report - still had the error.
Copied the text fields to a brand new report and added the database and command.
The error is now gone and the report is working.
Very strange.

query for databases and privileges

ok.. i am using sql server 2008... i want a sql query for finding different databases created by a particular login user??? for eg. lets say user1 has created databases db1 and db2. and user2 is created databases db3 and db4.. than the query should give result db1,db2 for user1 and db3,db4 for user2...
also i want query for getting the privileges of a particular user???
The closest I can think of is the current owner, which is very often the creator of a database. You can query that like:
select db.name
, l.name
from sys.databases db
join sys.syslogins l
on db.owner_sid = l.sid
To query for a comma-separated list per user, try:
select l.name
, stuff(db.list,len(db.list),1,'')
from sys.syslogins l
cross apply
(
select db.name + ','
from sys.databases db
where db.owner_sid = l.sid
for xml path('')
) db(list)
where db.list is not null
well I don't know why do you need to query the user owner for each database
but I hope this help you :
SELECT name, SUSER_SNAME(owner_sid)
FROM sys.databases
SUSER_SNAME() well retireve the user name