How to query across multiple databases - sql

I have a set of data that belongs to two different tables in two different databases. I have performed the query but I am getting a syntax error.
The two databases are:
dbo.Serve and dbo.Back
This is what I am doing:
SELECT Back.[dbo].Back$.Last_refreshed, dbo.Back.Backup_Name, dbo.Back$.Fle, dbo.Back$.Free_Space_TB, dbo.Backup$.Operational_status, dbo.Serve$.Name,
dbo.Back$.Rack_Enclosure, dbo.Back$.Server_Name, dbo.Back$.SIZE_TB, x.field, x.piv, dbo.Serve$.SKU,
CASE
WHEN dbo.Serve$.SKU like 'F1%' or dbo.Serve$.SKU like 'F2%' or dbo.Server_History$.SKU like 'F3%' or
dbo.Serve$.SKU like 'FY14%' or dbo.Serve$.SKU like 'F5%' or dbo.Server_History$.SKU like 'F6%' then 'Group1'
WHEN dbo.Serve$.SKU like 'Gr%' or dbo.Serve$.SKU like 'Fr%' or dbo.Server_History$.SKU like 'Ex%'
or dbo.Serve$.SKU like 'Ga%' or dbo.Serve$.SKU like 'Ho%' or dbo.Serve$.SKU like 'Gen%' then 'Group2'
END AS grp
FROM dbo.Back$
inner join dbo.Serve$ ON dbo.Serve$.Name = dbo.Back$.Server_Name and dbo.Back$.Last_refreshed = dbo.Serve$.Last_refreshed
cross apply (values (dbo.Back$.Free_Space_TB, 'Available'), (dbo.Back$.SIZE_TB - dbo.Back$.Available, 'Used')) AS x(field, piv)
Why would it give me an error on this part?
SELECT Back.[dbo].Back$.Last_refreshed,
as the syntax appears correct
Any help is appreciated.

This Back.[dbo].Back$.Last_refreshed,
does not make sense. You can still use databasename.schemaname.columnname
or a four part name (if you set up linked servers...
instancename.databasename.schemaname.columnname),
but it is not clear what you are referring to with: Back.[dbo].Back$.Last_refreshed

Related

SQL - Search multiple columns

I'm trying to search multiple columns for similar/matching values, while ensuring the "UID" is unique.
This seemed simple, but the following query ignores the "UID" and will return anything that matches under "SalesRef, "CustomerPO" or "Ref".
Any ideas why this would be?
SELECT * FROM OrderHeader
INNER JOIN DespatchDetails ON DespatchDetails.Ref = OrderHeader.Ref
INNER JOIN OrderStatus ON OrderStatus.Ref = OrderHeader.Ref
WHERE OrderHeader.UID = '$uid'
AND OrderStatus.SalesRef LIKE '%$search%'
OR OrderStatus.CustomerPO LIKE '%$search%'
OR OrderStatus.Ref LIKE '%$search%'
ORDER BY OrderHeader.OrderDate DESC";
There is a problem in the WHERE condition of yours. Consider to use brackets every time when you're using OR in WHERE part of the query
Let's have a closer look to your version
...
WHERE OrderHeader.UID = '$uid'
AND OrderStatus.SalesRef LIKE '%$search%'
OR OrderStatus.CustomerPO LIKE '%$search%'
...
To the database it says: get me all the lines where
WHERE OrderHeader.UID = '$uid'
AND OrderStatus.SalesRef LIKE '%$search%'
or
OrderStatus.CustomerPO LIKE '%$search%'
it means when this "OrderStatus.CustomerPO LIKE '%$search%'" condition is true, it will return a row without checking for the UID column and so on.
As far as I can understand the logic, all you need is to update the WHERE part of the query with the following
WHERE OrderHeader.UID = '$uid'
AND (OrderStatus.SalesRef LIKE '%$search%'
OR OrderStatus.CustomerPO LIKE '%$search%'
OR OrderStatus.Ref LIKE '%$search%')
ORDER BY OrderHeader.OrderDate DESC";

SQL problem with wildcards, strange behaviour

I have got this query:
SELECT Sites.Master_ID AS Master_ID, Scopes.Name AS Scope, Brands.Extension AS [Brand], Sites.ID AS [Site ID], Sites.Name AS [Site Name], Sites.Address, Sites.CAP, Sites.City, Countries.Name AS Country
From Sites
INNER Join Scopes ON Sites.scope_ID = Scopes.ID
INNER JOIN Brands ON Sites.brand_ID = Brands.ID
INNER Join Countries ON Sites.country_ID = Countries.ID
WHERE CONCAT (Scopes.Name, Brands.Extension, Sites.ID, Sites.Name, CONVERT(nvarchar(MAX),Sites.Address), Sites.CAP, Sites.City, Countries.Name) Like '%BA%'
That is producing the following result:
[
While if I remove a %, writing "Like 'BA%'" (instead of '%BA%') the SELECT is empty.
I really cannot understand the reason for that.
Thanks in advance for your help.
Regards,
The LIKE pattern matches the entire string.
So, '%BA%' looks for 'BA' anywhere in the string. But, 'BA%' looks only for strings that begin with 'BA'. And in your case, that probably means that scopes.name begins with 'BA'.
More recent versions of SQL Server support concat_ws() which allows you to do something like:
WHERE CONCAT_WS('|', '|', Scopes.Name, Brands.Extension, . . .) Like '%|BA%'
This would check that each column starts with 'BA'. It is something of a hack. It is probably better to be explicit:
WHERE Scopes.Name LIKE 'BA%' OR
Brands.Extension LIKE 'BA%' OR
. . .
Or perhaps:
WHERE 'BA' IN (LEFT(Scopes.Name, 2), LEFT(Brands.Extension, 2), . . .)

Multiple Likes in SQL

Please review the following code and help me in fixing the error.
When i'm trying to execute it i'm getting only the records which were starting with the ENTBI-Q.But the two conditions which were written below(task%,INC%) were not getting executed.
I want the records which starts with task,incident and entbi-q.
Please note that the task and incident are the field values of one column and the entbi-q is the field values of the another coloumn.
SELECT
S1."NAME" AS "NAME",
S1."SYS_ID" AS "SYSID",
T2."ASSIGNMENT_GROUP" AS "ASSIGNMENTGROUP",
T2."NUMBER_" AS "NUMBER",
T2."CLOSED_AT" AS "CLOSEDAT",
T2."OPENED_AT" AS "OPENEDAT"
FROM
"IOD"."SYS_USER_GROUP" S1,
"IOD"."TASK" T2
WHERE
(S1."SYS_ID"=T2."ASSIGNMENT_GROUP")
AND S1."NAME" LIKE 'ENTBI-Q%'
AND T2."NUMBER" LIKE 'TASK%'
AND T2."NUMBER" LIKE 'INC%'
AND T2."NUMBER" LIKE 'TASK%'
AND T2."NUMBER" LIKE 'INC%'
This can never be true. If a string starts with TASK it cannot start with INC. You probably wanted to OR the two conditions:
AND (T2."NUMBER" LIKE 'TASK%'
OR T2."NUMBER" LIKE 'INC%')

MS-Access query - Including Null values in with other conditions

Please help as this is aquery with many parameters but when it said
" ((OrderProdDetails.DeliveryDate) Like IIf([Forms]![ReportCriteriaSelection]![NoDevDate]=-1,"*","")) " it should have included null values aswell but it is not if I am putting or Is null with this criteria then it is being applied for both conditions regardless of check box is checked or not. Please help getting the null values included when checkbox is checked.
Please have look on screenshots:
Query in design View
Criteria Selection Screen
Query:
SELECT OrderCustMain.OdrID, OrderCustMain.CustName, OrderCustMain.CustEmail,
OrderCustMain.OdrDate, OrderProdDetails.DeliveryDate, Date()-
[OrderCustMain]![OdrDate] AS [Days Past], OrderCustMain.OrderStatus,
OrderProdDetails.BrandName, OrderProdDetails.ModelName,
OrderProdDetails.Priority, SupplierDetails.SupEmail, OrderProdDetails.Status
FROM SupplierDetails INNER JOIN (OrderCustMain INNER JOIN OrderProdDetails
ON OrderCustMain.[OdrID] = OrderProdDetails.[OrdID]) ON
SupplierDetails.SupOrgName = OrderProdDetails.BrandName WHERE
(((OrderCustMain.OdrDate) Like IIf([Forms]![ReportCriteriaSelection]!
[NoStDate]=-1,"*","")) AND ((OrderProdDetails.DeliveryDate) Like
IIf([Forms]![ReportCriteriaSelection]![NoDevDate]=-1,"*","")) AND ((Date()-
[OrderCustMain]![OdrDate])>=[Forms]![ReportCriteriaSelection]![cboDaysPast])
AND ((OrderCustMain.OrderStatus) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) OR
(((OrderCustMain.OdrDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritOdrStDate] And (OrderCustMain.OdrDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritOdrEndDate]) AND
((OrderProdDetails.DeliveryDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritDlvryStDate] And (OrderProdDetails.DeliveryDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritDlvryEndDate]) AND ((Date()-
[OrderCustMain]![OdrDate])>=[Forms]![ReportCriteriaSelection]![cboDaysPast])
AND ((OrderCustMain.OrderStatus) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) OR (((OrderCustMain.OdrDate)
Like IIf([Forms]![ReportCriteriaSelection]![NoStDate]=-1,"*","")) AND
((OrderProdDetails.DeliveryDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritDlvryStDate] And (OrderProdDetails.DeliveryDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritDlvryEndDate]) AND ((Date()-
[OrderCustMain]![OdrDate])>=[Forms]![ReportCriteriaSelection]![cboDaysPast])
AND ((OrderCustMain.OrderStatus) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) OR
(((OrderCustMain.OdrDate)>=[Forms]![ReportCriteriaSelection]!
[RepCritOdrStDate] And (OrderCustMain.OdrDate)<=[Forms]!
[ReportCriteriaSelection]![RepCritOdrEndDate]) AND
((OrderProdDetails.DeliveryDate) Like IIf([Forms]![ReportCriteriaSelection]!
[NoDevDate]=-1,"*","")) AND ((Date()-[OrderCustMain]![OdrDate])>=[Forms]!
[ReportCriteriaSelection]![cboDaysPast]) AND ((OrderCustMain.OrderStatus)
Like IIf(IsNull([Forms]![ReportCriteriaSelection]![RepCritOdrStatus]),"*",
[Forms]![ReportCriteriaSelection]![RepCritOdrStatus])) AND
((OrderProdDetails.BrandName) Like IIf(IsNull([Forms]!
[ReportCriteriaSelection]![RepCritBrandName]),"*",[Forms]!
[ReportCriteriaSelection]![RepCritBrandName]))) ORDER BY
OrderCustMain.OdrID, OrderProdDetails.Priority;
The primary source for your trouble is that dates are not strings, so "Date Like some-string" will either fail or return unintended results.
You'll have to redesign this from scratch. Exactly how depends on the context.
Would something like this help?
WHERE Nz(OrderCustMain.OdrDate, #1/1/1900#) >= IIf([Forms]![ReportCriteriaSelection]!
[NoStDate]=-1,Nz(OrderCustMain.OdrDate, #1/1/1900#),[Forms]![ReportCriteriaSelection]!
[RepCritOdrStDate])
It might simplify the code.

Grouping multiple 'AND x not like y' In SQL statement

I currently have an SQL statement that is too long for my program (I have a maximum number of character that I can use. I'm using sccm report). The problem is my SQL statement look like this:
Select distinct v_GS_ADD_REMOVE_PROGRAMS_64.DisplayName0, v_GS_ADD_REMOVE_PROGRAMS_64.Publisher0, v_GS_ADD_REMOVE_PROGRAMS_64.Version0
FROM v_GS_ADD_REMOVE_PROGRAMS_64
JOIN v_R_System ON v_GS_ADD_REMOVE_PROGRAMS_64.ResourceID = v_R_System.ResourceID
WHERE (v_R_System.Netbios_Name0 = #computername)
DisplayName0 NOT LIKE 'hpp%'
AND
DisplayName0 NOT LIKE 'Logitech SetPoint%'
AND
DisplayName0 NOT LIKE 'HP Document Manager%'
AND
DisplayName0 NOT LIKE 'HP Imaging Device Functions%'
AND
DisplayName0 NOT LIKE 'PyQt4 - PyQwt5%'
And it goes on and on for 20 pages. How can I minimize the amount of code this request contains? Is there a way to group all the displayName0 not like ?? with something like a NOT IN(value1, value2, ...)?
If you are OK without tailing % in you pattern you can replace it with:
SELECT ... WHERE DisplayName0 NOT IN ('hpp','Logitech SetPoint','HP Document Manager',...)
It would make it somehow shorter.
But it seems to me that proper solution would be to create [temp] table with all the names you need to filter against and then join it.
Could you store the values in a separate table and then reference it in your query like this?:
SELECT DISTINCT v_GS_ADD_REMOVE_PROGRAMS_64.DisplayName0
,v_GS_ADD_REMOVE_PROGRAMS_64.Publisher0
,v_GS_ADD_REMOVE_PROGRAMS_64.Version0
FROM v_GS_ADD_REMOVE_PROGRAMS_64
JOIN v_R_System ON v_GS_ADD_REMOVE_PROGRAMS_64.ResourceID = v_R_System.ResourceID
WHERE (v_R_System.Netbios_Name0 = #computername) DisplayName0 NOT IN (
SELECT DisplayName0
FROM < NewTableName >
)