Accessing Active Directory Role Membership through LDAP using SQL Server 2005 - sql-server

I would like to get a list of Active Directory users along with the security groups they are members of using SQL Server 2005 linked servers. I have the query working to retrieve records but I'm not sure how to access the memberOf attribute (it is a multi-value LDAP attribute).
I have this temporary to store the information:
DROP TABLE #ADUSERGROUPS
CREATE TABLE #ADUSERGROUPS
(
sAMAccountName varchar(30),
UserGroup varchar(50)
)
Each group/user association should be one row.
This is my SELECT statement:
SELECT sAMAccountName,memberOf
FROM OpenQuery(ADSI, '<LDAP://hqdc04/DC=nt,DC=avs>;
(&(objectClass=User)(sAMAccountName=9695)(sn=*)(mail=*)(userAccountControl=512));
sAMAccountName,memberOf;subtree')
I get this error msg:
OLE DB error trace [OLE/DB Provider 'ADSDSOObject' IRowset::GetData returned 0x40eda: Data status returned from the provider: [COLUMN_NAME=memberOf STATUS=DBSTATUS_E_CANTCONVERTVALUE], [COLUMN_NAME=sAMAccountName STATUS=DBSTATUS_S_OK]].
Msg 7346, Level 16, State 2, Line 2
Could not get the data of the row from the OLE DB provider 'ADSDSOObject'. Could not convert the data value due to reasons other than sign mismatch or overflow.

Looks like this is a limitation that cannot be directly overcome: - TSQL: How to get a list of groups that a user belongs to in Active Directory. OpenQuery cannot handle multi-valued attributes.
I ended up writing a .NET CLR job to handle this.

Related

SQL Server -> Linked Server to Oracle: "inconsistent metadata" on n+1st query

I use LinkedServer to connect SQL-Server with Oracle.
No matter if I use openquery or not, I can only query one single action like "delete" or "insert" on my LinkedServer-Connection. For the n+1st query, I get the following error from SQL-DB:
The OLE DB provider "OraOLEDB.Oracle" for linked server "NAME"
supplied inconsistent metadata. An extra column was supplied during
execution that was not found at compile time.
When I check sessions on OracleDB, I can see that immediately after my query, there is an inactive session from the SQL-Server-Host with a wait-event "SQL*Net message from client". This inactive session disappears after about 60s. Until then, I cannot query any other actions on my LinkedServer-Connection - unless I kill this session manually.
How can I prevent this session from being created and blocking further queries?
Linked-Server-Config:
Provider: Oracle Provider for OLE DB
Provider-Options: "Level zero only" = TRUE, "Allow inprocess" = TRUE
Linked-Server Options: default
Thanks in advance,
Martin
EDIT: Minimal-Repro
At first, I insert data into empty oracle table via
INSERT OPENQUERY
(LINK, 'select COLUMNS from LINKED_TABLE') VALUES (VALUES);
This works.
Next, I insert more data into oracle table, e.g.
INSERT OPENQUERY
(LINK, 'select COLUMNS from LINKED_TABLE') VALUES (DIFFERENT_VALUES);
Now, I get the following error in SSMS:
Msg 7353, Level 16, State 1, Line 5
The OLE DB provider "OraOLEDB.Oracle" for linked server "LINK" supplied inconsistent metadata. An extra column was supplied during execution that was not found at compile time.
If I check oracle sessions, I can see that there is an inactive connection-session for my SQL-Server-Host. Only if I kill this connection-session in Oracle SQL Developer, I can insert (or delete/update) more data. "Select" works without problems.
Hope this helps.

Inserting into a table from view create witha link server

I have created a view that includes a view from a linked server and I am inserting into a table. when I query the table it returns no data when the where statement is referencing data from the linkserver table. It does however pull all the data when not using the where statement. why is this for I do not have much experience working with LinkedServers.
Here is code I used to build my view i am using to populate my table.
ALTER VIEW [dbo].[weightsDashboard]
AS
SELECT *
FROM [dbo].[weightsYak]
UNION ALL
SELECT *
FROM OPENQUERY([10.3.50.62\AGJET], 'SELECT * FROM [CheckWeigher].
[dbo].[weightsSs]')
Then I ran this to create the dataset I want to report off of.
select *
into Dataset_SaleSummary
from [dbo].[weightsDashboard]
Thanks in advance.
The root cause Would be driver. Use ODBC instead of OLE DB.If you are trying to connect oracle database from sql server
Delete the linked server from SQL Server
Right click on the "Linked Servers" folder and select "New Linked Server..."
Linked Server: enter anything..this will be the name of your new linked server
Provider: Select "Oracle Provider for OLE DB"
Product Name: enter "Oracle" (without the double quotes)
Data Source: enter the alias from your TNSNAMES.ORA file.
Provider String: leave it blank
Location: leave it blank
Catalog: leave it blank
Now go to the "Security" tab, and click the last radio button that says "Be made using this security context:" and enter the username & password for your connection.
I had exact same problem with an SQL 2014 getting data from SQL 2000 through OPENQUERY. Because ODBC compatibility problem, I had to keep generic OLE DB for ODBC driver. Moreover, the problem was only with SQL non-admin account. So finally, the solution I found was to add SET ROWCOUNT 0:
SELECT * FROM OPENQUERY(DWH_LINK, 'SET ROWCOUNT 0 SELECT * FROM TABLEA ')
It seems the rowcount might been change somewhere through the SQL procedure (or for this user session), so setting it to 0 force it to return "all rows".

Query from linked SQL Server returns error if it has a "dot" in the table name

Selecting data from a linked SQL Server returns an error when there is a period in the name of the table.
SELECT *
FROM [LEGOSDB].[LegosDB].[dbo].[MY.Employees]
Results in the following error message.
Msg 7314, Level 16, State 1, Line 2
The OLE DB provider "SQLNCLI10" for linked server "LEGOSDB" does not contain the table ""LegosDB"."dbo"."MY.Employees"". The table either does not exist or the current user does not have permissions on that table.
It works fine when I run a query without a period in the table name.
SELECT *
FROM [LEGOSDB].[LegosDB].[dbo].[BankingInfo]

Importing Excel Data into SQL Server 2008 R2 Express

There seems to have been many discussions on this but I couldn't find something specific to what I am looking for. I am trying to use a query to import data from Excel to an existing table in SQL Server.
My query is:
INSERT INTO DailyRawData
SELECT *
FROM OPENROWSET('Microsoft.ACE.OLEDB.12.0',
'Excel 12.0 Xml; HDR=NO;
Database=C:\Users\home\Desktop\SQLImportTrim.xls', [data$]);
I get the following error:
Msg 7314, Level 16, State 1, Line 2
The OLE DB provider "Microsoft.ACE.OLEDB.12.0" for linked server "(null)" does not contain the table "data$". The table either does not exist or the current user does not have permissions on that table.
I don't think this is a permission issue for me as I am set up as SysAdmn. I am wondering if the error is due to the last part of the query [data$] since this is what the error msg refers to. FYI the name of the excel file is SQLImportTrim and the tab that contains all my data is named data. There is no table named data in my Excel file. Is my query correct?
you don't use [data$] you use the name of the sheet, so the standard starting point is usually [Sheet1$] if you haven't renamed it.

`Invalid object name` unless I specify database name in SQL Server 2008 queries

Up until today I've been able to run queries without using the [databaseName].[dbo].[fieldName] syntax. And all of a sudden, if I use select * from myTable I get an error for an invalid object. I can't possibly think of something that happened between shutting down my PC yesterday and today. Anyone know anything about this?
Msg 208, Level, 16, State 1 Line 1
Invalid object name 'mytable'
It's only been since today that I have to include the database name in the query. There are no other connections open and no other users of this instance of SQL Server.
I think you mean a query of the form select [fieldName] from [databaseName].[dbo].[mytable]
Here are some possible things to look out for:
Make sure that you are in the correct database context / catalogue (i.e. use [databasename], or select the correct database from the Available Databases drop down in SSMS)
Ensure that if you have a case sensitive collation on your database that the object names in your query match the exact case.
Check that the default schema for your user hasn't changed on this database. Although the default schema is usually [dbo], it can be changed.
Edit : More ideas:
Do SELECT DB_NAME() to see what the current database name is.
Check to see if someone has dropped the table or view entirely, e.g. from the target database, run:
Select * from sysobjects where name = 'myobject'
OR
Select * from sys.tables where name = 'mytable'
OR
Select * from INFORMATION_SCHEMA.TABLES

Resources