Showing posts with label mssql. Show all posts
Showing posts with label mssql. Show all posts

Wednesday, March 28, 2012

Linking 2 Databases

How do i get a query in 1 database to fetch results from a table in another database?
I am using MSSQL 2005I hope ur question is like this

You have 2 database DB1 and DB2. You want to select Tab2 in DB2 while you are executing the query in DB1.

The query would be like this

Select *
From DB2.dbo.Tab2

the general syntax would be

Select <column1>,<column2>......
from <database_name>.dbo.<table_name>

Friday, March 23, 2012

servers to UDB via IBMDADB2, access denied

A linked server definition in MSSQL 2000, can succcessfully authenticates
with a UDB database v8.1.0.61 on a SUN box, but then gets access denied,
eventhough the account being used has DBADM authority. However, I can
successfully authenticate and query the same UDB databases from a DOS DB2
command prompt on the same windows 2003 server.
See example below and results:
On the windoes 2003 server...
set nocount on
EXEC sp_addlinkedserver
@.server = 'srvr1833',
@.srvproduct = 'aeplm148',
@.provider = 'IBMDADB2',
@.datasrc = 'aeplm148',
@.location = 'srvr1833.dbms.gdomain.com:18330',
@.catalog = 'aeplm148'
go
EXEC sp_addlinkedsrvlogin
@.rmtsrvname = 'srvr1833',
@.useself = false,
@.rmtuser = 'User1',
@.rmtpassword = 'Password1'
go
EXEC sp_serveroption
@.server = 'srvr1833',
@.optname = 'data access',
@.optvalue = 'true'
go
SELECT *
FROM srvr1833.aepcclm148.epl.mline
Results:
Server: Msg 7399, Level 16, State 1, Line 1
OLE DB provider 'IBMDADB2' reported an error. Access denied.
OLE DB error trace [OLE/DB Provider 'IBMDADB2' IUnknown::QueryInterface
returned 0x80070005: Access denied.].
Yet on a DOS command line using db2...
db2 => connect to aeplm148 user User1 using Password1
Database Connection Information
Database server = DB2/SUN 8.1.6
SQL authorization ID = DGIEPL
Local database alias = AEPLM148
db2 => list database directory
System Database Directory
Number of entries in the directory = 1
Database 1 entry:
Database alias = AEPLM148
Database name = AEPLM148
Node name = XX
Database release level = a.00
Comment =
Directory entry type = Remote
Catalog database partition number = -1
Alternate server hostname =
Alternate server port number =
db2 => list node directory show detail
Node Directory
Number of entries in the directory = 1
Node 1 entry:
Node name = XX
Comment =
Directory entry type = LOCAL
Protocol = TCPIP
Hostname = srvr1833.dbms.gdomain.com
Service name = 18330
Remote instance name =
System =
Operating system type = None
db2 => select * from aeplm148.epl.mline fetch first 2 rows only
I_PLANT_PL I_STDY I_PRES_LINE I_PRES_TYP N_LINE_NAME
L_PLAN I_LOAD_TYP I_AUTOM_TYP I_PRES_LINE_SIZE I_PRES_MECHNM_TYP
L_
TCH_PL_LINE_TO_PLT_LINE Q_SHUT_HGT Q_NMBR_OF_STATNS I_UPD_TID D_UPD_LAST
X_COMN
More results followed but not pasted...
Can anybody offer some suggestions?
I've run out of ideas.
I know the linked server is authenticating properly because when I put in a
bad password, the error message returned says, bad password or userid.
One would assume that the problem is on the UDB server only. Yet I can
access the data from the Windows 2003 server using the UDB client in a DOS
prompt.
Both techniques authenticate properly. Only the linked server gets access
denied.
The driver version on the Windows 2003 Server is DB2 v8.1.12.99A clue was found.
This will work when you remote desktop to the server and log in as either
the administrators account to the account in which the database engine is
running under. But it will not work with any other account, even if that
account is part of the administrators group.
What is different about the "Administrator" account from an a account that
is simply a member of the Administor"s" group?
The account used by the sqlserver engine is also part of the administrators
group.
"DBA449" wrote:

> A linked server definition in MSSQL 2000, can succcessfully authenticates
> with a UDB database v8.1.0.61 on a SUN box, but then gets access denied,
> eventhough the account being used has DBADM authority. However, I can
> successfully authenticate and query the same UDB databases from a DOS DB2
> command prompt on the same windows 2003 server.
> See example below and results:
> On the windoes 2003 server...
> set nocount on
> EXEC sp_addlinkedserver
> @.server = 'srvr1833',
> @.srvproduct = 'aeplm148',
> @.provider = 'IBMDADB2',
> @.datasrc = 'aeplm148',
> @.location = 'srvr1833.dbms.gdomain.com:18330',
> @.catalog = 'aeplm148'
> go
> EXEC sp_addlinkedsrvlogin
> @.rmtsrvname = 'srvr1833',
> @.useself = false,
> @.rmtuser = 'User1',
> @.rmtpassword = 'Password1'
> go
> EXEC sp_serveroption
> @.server = 'srvr1833',
> @.optname = 'data access',
> @.optvalue = 'true'
> go
> SELECT *
> FROM srvr1833.aepcclm148.epl.mline
> Results:
> Server: Msg 7399, Level 16, State 1, Line 1
> OLE DB provider 'IBMDADB2' reported an error. Access denied.
> OLE DB error trace [OLE/DB Provider 'IBMDADB2' IUnknown::QueryInterfac
e
> returned 0x80070005: Access denied.].
>
> Yet on a DOS command line using db2...
>
> DB2 => connect to aeplm148 user User1 using Password1
> Database Connection Information
> Database server = DB2/SUN 8.1.6
> SQL authorization ID = DGIEPL
> Local database alias = AEPLM148
> DB2 => list database directory
> System Database Directory
> Number of entries in the directory = 1
> Database 1 entry:
> Database alias = AEPLM148
> Database name = AEPLM148
> Node name = XX
> Database release level = a.00
> Comment =
> Directory entry type = Remote
> Catalog database partition number = -1
> Alternate server hostname =
> Alternate server port number =
> DB2 => list node directory show detail
> Node Directory
> Number of entries in the directory = 1
> Node 1 entry:
> Node name = XX
> Comment =
> Directory entry type = LOCAL
> Protocol = TCPIP
> Hostname = srvr1833.dbms.gdomain.com
> Service name = 18330
> Remote instance name =
> System =
> Operating system type = None
> DB2 => select * from aeplm148.epl.mline fetch first 2 rows only
> I_PLANT_PL I_STDY I_PRES_LINE I_PRES_TYP N_LINE_NAME
> L_PLAN I_LOAD_TYP I_AUTOM_TYP I_PRES_LINE_SIZE I_PRES_MECHNM_T
YP
> L_
> TCH_PL_LINE_TO_PLT_LINE Q_SHUT_HGT Q_NMBR_OF_STATNS I_UPD_TID D_UPD_LAST
> X_COMN
> More results followed but not pasted...
> Can anybody offer some suggestions?
> I've run out of ideas.
> I know the linked server is authenticating properly because when I put in
a
> bad password, the error message returned says, bad password or userid.
> One would assume that the problem is on the UDB server only. Yet I can
> access the data from the Windows 2003 server using the UDB client in a DOS
> prompt.
> Both techniques authenticate properly. Only the linked server gets access
> denied.
> The driver version on the Windows 2003 Server is DB2 v8.1.12.99
>sql

Friday, March 9, 2012

server, heterogenous query

Hi all!

I have to pull new data from an Ingres Server over to my MSSQL server. I set the Ingres box up as a linked server and I can do the following:

declare @.start as datetime

set @.startdate = (select start from synctimes)

select * from openquery(INGRES, 'select * from ingrestable') where date > @.start

This does exactly what I want in my testsetup. In real life, the two boxen are connected via a slow connection and I wonder if that way *all* records are transferred and filtered on the MSSQL side, thus I transfer 99.5% rubbish. Is that true and ifso, is there any way to get around this?

in addition, I want to acheive this

CREATE PROCEDURE import_all AS
declare @.myVariable as Cursor
declare @.start as datetime
declare @.tblname as nvarchar(50)

declare myCursor Cursor for
SELECT tblname from synctables

open myCursor

set @.myVariable = myCursor

fetch next from @.myvariable into @.tblname
while @.@.fetch_status = 0
begin
select * from openquery(INGRES, 'select * from @.table where date > @.start')
fetch next from @.myVariable into @.tblname
end
close myCursor
deallocate myCursor

Is this possible?

TIA,

chrisI wonder if that way *all* records are transferred and filtered on the MSSQL side, thus I transfer 99.5% rubbish. Is that true and ifso, is there any way to get around this?
Yup, you are transfering all the records and THEN filtering, try:

declare @.startdate as datetime, @.TSQL as varchar(255)
select @.startdate = getdate()
set @.TSQL = 'select * from openquery(INGRES, ''select * from ingrestable where date > ''' + convert(varchar(25),@.startdate,121) + ''')'
print @.TSQL
exec(@.TSQL)

I don't exactly recall what date format Ingres uses but you can adjust the convert as needed.

as for your sp, I think you are on the right track but I see some problems. Consider:

CREATE PROCEDURE import_all(
@.Debug bit = 0)
AS

declare @.start as datetime
, @.tblname as nvarchar(50)
, @.TSQL as varchar(255)

select @.tblname = min(tblname from synctables
while (@.tblname is not null) begin
set @.TSQL = 'select * from openquery(INGRES, ''select * from ' + @.table + ' where date ''' + convert(varchar(25),@.startdate,121) + ''')'
if (@.Debug > 0)
raiserror('%s',0,1,@.TSQL)
else
exec(@.TSQL)
select @.tblname = min(tblname from synctables where tblname > @.tblname
end

return 0

Of course I have no way of test this, on your first run you may want to set @.Debug to 1 to check the resulting code.|||Thanks alot. Seems to be just what I needed. Couldn't test it though because of a defective ingres odbc driver I won't reinstall before monday or tuesday. I'll post my results afterwards ;-)