SQL Server Linked Server openquery for WHERE clause - sql-server

I am trying to write a query to get the delta between my server and Linked Server. So I wrote the following which worked until:
Select MD5 FROM ServerA A
WHERE NOT EXISTS
(
SELECT 1
FROM [LinkedServer].[Database].[dbo].[Files] fi
WHERE A.MD5High = fi.MD5High AND A.MD5Low = fi.MD5Low
)
However this pulls all the data from LinkedServer to my server which causes my server to run out of resources. So my next attempt was to perform the query on the linked server
Select MD5 FROM ServerA A
WHERE NOT EXISTS
(
SELECT 1
FROM OPENQUERY( [LinkedServer] , 'SELECT [LinkedServer].[Database].[dbo].[Files] fi
WHERE fi.MD5High = ' + A.MD5Low + ' AND fi.MD5Low= ' + a.MD5Low + '')
)
However the syntax in the second query is incorrect and I can't figure the correct syntax.
I appreciate any direction

Related

snowflake get list of table given the database

I am using snowflake jdbc driver to get the list of tables in a given database,
i have 2 prepared statement. I get the PreparedStatement from the same Connection
PreparedStatement usePrepStatement = con.prepareStatement("use database IDENTIFIER(:1)");
usePrepStatement.setString(1, database);
usePrepStatement.execute();
and to get the tables from information_schema
String sql = "select table_schema, \n" +
" table_name, \n" +
" table_type \n" +
"from information_schema.tables where table_catalog = '?'";
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, database);
ResultSet tableResultset = preparedStatement.executeQuery();
Here tableResultset is empty. How to I get this query to return the right result
I can query the current_database() I see the current database is set to the right value
The above queries works if i use Statement instead of PreparedStatement.
Identifiers are stored in uppercase, so using case insensitive comparison should solve it.Also there is no need to wrap the parameter with ':
from information_schema.tables where table_catalog = '?'
=>
from information_schema.tables where table_catalog ILIKE ?

Problem getting data from linked server error in SQL

I m working in SQL and I have few tables in Oracle database from which I have to get data, so I m doing so via linked server. If I omit date filter then the query works fine but if I apply date filter then it breaks.
This query works fine.
select * from openquery([ORCL], 'select sls_value, retailer_cd, tery_cd, inv_dt
from Toc_cust_second_sls_new where cust_cd IN ( ''1-ZUT42P'' )') secSls --- This query works fine
This query breaks
select * from openquery([ORCL], 'select sls_value, retailer_cd, tery_cd, inv_dt from Toc_cust_second_sls_new where cust_cd IN ( ''1-ZUT42P'' )
and to_date(inv_dt, ''dd/mm/yyyy'') between to_date(''01/05/2020'', ''dd/mm/yyyy'') and to_date(''31/05/2020'', ''dd/mm/yyyy'')') secSls
I get this error.
OLE DB provider "OraOLEDB.Oracle" for linked server "ORCL" returned message "ORA-01861: literal does not match format string".
To filter date I can use this query as it works fine.
select * from openquery([ORCL], 'select sls_value, retailer_cd, tery_cd, inv_dt, inv_no, cust_cd from Toc_cust_second_sls_new
where cust_cd IN ( ''1-ZUT42P'' )') secSls
where convert(date,secSls.inv_dt, 101) >= '2020-05-01' and convert(date,secSls.inv_dt, 101) <= '2020-05-31'
But the problem in the above query is, it first gets all the data from oracle then it filters so there are hundreds if thousands of records which takes a lot of time.
I solved it.
I removed the conversion of inv_dt in oracle as it's data type was already Date, trying to again convert it was creating problem.
select * from openquery([ORCL], 'select sls_value, retailer_cd, tery_cd, inv_dt from Toc_cust_second_sls_new where cust_cd IN ( ''1-ZUT42P'' ) and inv_dt between to_date(''01/05/2020'', ''dd/mm/yyyy'') and to_date(''31/05/2020'',''dd/mm/yyyy'')') secSls
This query works now. Thanks guys who helped me out on comments section.

Obtain the Filename for the Current Running SQL script - MS SQL Server 2008

I have a system that auto-generates SQL files for me in the format:
CALLID_DATE_SCRIPTYPE.sql - eg 108312_20170913_DeleteRubbishData.sql
I want to grab the call id and current user automatically without having to specify it again.
I know how to grab user, but not sure if it is possible to grab filename.
Anyone know if this is possible?
EXAMPLE:
DECLARE #File NVARCHAR(100) = CURRENTFILENAMEORWHATEVERTHEFUNCTIONISCALLED();
DECLARE #AuditData NVARCHAR(100) = 'Call: ' + LEFT(#File, CHARINDEX('_', #File) - 1)
+ ', User: ' + SUSER_NAME();
PRINT #AuditData
OUTPUT:
Call: 108312, User: Sys\JSmith

How to determine who performed DROP/DELETE on Sql Server database objects?

There is always a need to find out details, either intentionally Or mistakenly someone executed DROP/DELETE command on any of following SQL Server database objects.
DROPPED - Table from your database
DROPPED - Stored Procedure from your database
DELETED - Rows from your database table
Q. Is there TSQL available to find db user who performed DELETE/DROP?
Q. What kind of permissions are needed for user to find out these details?
Did you check this ?
Right click on database.
Go to as shown in image :
Solution 2 :
This query gives alot of useful information for a database(apply filter as required) :
DECLARE #filename VARCHAR(255)
SELECT #FileName = SUBSTRING(path, 0, LEN(path)-CHARINDEX('\', REVERSE(path))+1) + '\Log.trc'
FROM sys.traces
WHERE is_default = 1;
SELECT gt.HostName,
gt.ApplicationName,
gt.NTUserName,
gt.NTDomainName,
gt.LoginName,
--gt.SPID,
-- gt.EventClass,
te.Name AS EventName,
--gt.EventSubClass,
-- gt.TEXTData,
gt.StartTime,
gt.EndTime,
gt.ObjectName,
gt.DatabaseName,
gt.FileName,
gt.IsSystem
FROM [fn_trace_gettable](#filename, DEFAULT) gt
JOIN sys.trace_events te ON gt.EventClass = te.trace_event_id
WHERE EventClass in (164) --AND gt.EventSubClass = 2
ORDER BY StartTime DESC;

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.

Resources