Use a specific database depending on condition - sql

Can I somehow use a specific database given a specific condition? To clarify I will give a naive example:
CASE
WHEN #dbnum = 1 THEN USE Db1
ELSE USE DefaultDb
END

You can do it with an IF:
IF #dbnum = 1
USE Db1;
ELSE
USE DefaultDb;

Related

exclude items based on field value

I've created a view using the below script but one of the fields is pulling back information that I don't need. Within the 'IsServer' column, items that are servers are marked with a 1 and items that aren't servers are marked with a 0. Is there a way of creating the view and exclude items that are servers that are marked as 1 in the IsServer column? I'm unsure how to build this into the create view script.
CREATE VIEW [dbo].[vw_Cherwell_Machines] AS
SELECT dbo.rptComputers.HostName,
dbo.rptComputers.Vendor,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.ProcessorCount,
dbo.tblComputer.IsServer,
dbo.rptComputers.BiosSerialNumber,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress AS 'MultipleIPAddress',
CASE WHEN PATINDEX ('%[, ]%',dbo.rptComputers.IPAddress) > 0 THEN
left(dbo.rptComputers.IPAddress,(PATINDEX('%[, ]%',dbo.rptComputers.IPAddress))-1)
ELSE dbo.rptComputers.IPAddress
END AS 'IPAddress',
dbo.rptComputers.LastScanDate
FROM dbo.rptComputers INNER JOIN dbo.tblComputer ON dbo.rptComputers.CID = dbo.tblComputer.CID
AND dbo.rptComputers.ComputerID = dbo.tblComputer.ComputerID
GROUP BY dbo.rptComputers.HostName,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.Vendor,
dbo.rptComputers.ProcessorCount,
dbo.rptComputers.BiosSerialNumber,
dbo.tblComputer.IsServer,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress,
dbo.rptComputers.LastScanDate
You need a filtered view; i.e., a view with a WHERE clause. Something like this (I marked off in comments the begin/end of my addition to your code):
CREATE VIEW [dbo].[vw_Cherwell_Machines] AS
SELECT dbo.rptComputers.HostName,
dbo.rptComputers.Vendor,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.ProcessorCount,
dbo.tblComputer.IsServer,
dbo.rptComputers.BiosSerialNumber,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress AS 'MultipleIPAddress',
CASE WHEN PATINDEX ('%[, ]%',dbo.rptComputers.IPAddress) > 0 THEN
left(dbo.rptComputers.IPAddress,(PATINDEX('%[, ]%',dbo.rptComputers.IPAddress))-1)
ELSE dbo.rptComputers.IPAddress
END AS 'IPAddress',
dbo.rptComputers.LastScanDate
FROM dbo.rptComputers INNER JOIN dbo.tblComputer ON dbo.rptComputers.CID = dbo.tblComputer.CID
AND dbo.rptComputers.ComputerID = dbo.tblComputer.ComputerID
/*begin change*/
WHERE tblComputer.IsServer = 0
/*end change*/
GROUP BY dbo.rptComputers.HostName,
dbo.tblComputer.IsVirtual,
dbo.rptComputers.Vendor,
dbo.rptComputers.ProcessorCount,
dbo.rptComputers.BiosSerialNumber,
dbo.tblComputer.IsServer,
dbo.rptComputers.PhysicalMemory,
dbo.rptComputers.ProcessorType,
dbo.rptComputers.ProcessorSpeed,
dbo.rptComputers.MostFrequentUser,
dbo.tblComputer.Domain,
dbo.rptComputers.ClientVersion,
dbo.rptComputers.ClientInstallDate,
dbo.rptComputers.ClientConfigurationName,
dbo.rptComputers.BiosVersion,
dbo.rptComputers.BiosDate,
dbo.rptComputers.Manufacturer,
dbo.rptComputers.Model,
dbo.rptComputers.IsPortable,
dbo.rptComputers.OperatingSystem,
dbo.tblComputer.OSServicePack,
dbo.rptComputers.ComputerStatusCode,
dbo.rptComputers.IPAddress,
dbo.rptComputers.LastScanDate
Documentation: https://learn.microsoft.com/en-us/sql/relational-databases/indexes/create-filtered-indexes?view=sql-server-ver15 Be sure to see the "limitations" section there, in case there's something in your environment which will not work here.

Google Bigquery multiple updates based on WHERE - need a solution

is there a way to do multiple updates based on other field value
WHERE, not CASE
idea is below
thanks
#standardSQL
UPDATE dataset.people
SET CBSA_CODE = '54620' where substr(zip,1,5) = '99047',
SET CBSA_CODE = '31793' where substr(zip,1,5) = '45700'
A CASE expression is in fact the typical way you would handle this logic:
UPDATE dataset.people
SET CBSA_CODE = CASE SUBSTR(zip, 1, 5)
WHEN '99047' THEN '54620'
WHEN '45700' THEN '31793' END
WHERE
SUBSTR(zip, 1, 5) IN ('99047', '45700');
The only alternative to this which I can see would be to run mutliple update statements, one for each ZIP code value. But that seems unwieldy and undesirable as compared to using a CASE expression.
Use SQL CASE, which is part of Standard SQL (see official BQ docs):
#standardSQL
UPDATE dataset.people
SET CBSA_CODE = CASE
WHEN substr(zip,1,5) = '99047' THEN '54620'
WHEN substr(zip,1,5) = '45700' THEN '31793'
END
WHERE substr(zip,1,5) IN('99047', '45700')

How to use If statement in addCondition, YII

I have some conditions to hand on to my dataprovider. Now, my last condition I want only to take place only when the field "edit" is set to true -> In this case I want to check if the editConfirmed field 'editBevestigd' is set to true. If the 'edit' field is empty I don't want to add this last condition.
$criteria->addCondition('bevestigd = 1');
$criteria->addCondition('IF(edit = 1) editBevestigd = 1');
What is the best way to handle this. Can I do this in YII (problem here is that the record is not known yet). Or how do I write this in SQL (I know this last condition isn't right right now..)?
Thanks in advance!
If i right understand what you want then you should use this condition:
WHERE (edit = 1 AND editBevestigd = 1) OR edit = 0
Thus, the condition becomes:
$criteria->addCondition('(edit = 1 AND editBevestigd = 1) OR edit = 0');
You can try like that
//for php variable set or not
if($edit==1){
$criteria->addCondition('editBevestigd=1');
}
//for column set or not
$criteria->addCondition('edit IS NOT NULL AND editBevestigd=1');

How to select if a row exists in HQL

EDIT: Specifically talking about querying against no table. Yes I can use exists, but I'd have to do
select case when exists (blah) then 1 else 0 end as conditionTrue
from ARealTableReturningMultipleRows
In T-SQL I can do:
select case when exists(blah) then 1 else 0 end as conditionTrue
In Oracle I can do:
select case when exists(blah) then 1 else 0 end as conditionTrue from DUAL
How can I achieve the same thing in HQL?
select count() seems like the second-best alternative, but I don't want to have to process every row in the table if I don't need to.
Short answer: I believe it's NOT possible.
My reasoning:
According to Where can I find a list of all HQL keywords? Hibernate project doesn't publish HQL grammar on their website, it's available in the Hibernate full distribution as a .g ANTLR file though.
I don't have much experience with .g files from ANTLR, but you can find this in the file (hibernate-distribution-3.6.1.Final/project/core/src/main/antlr/hql.g):
selectFrom!
: (s:selectClause)? (f:fromClause)? {
// If there was no FROM clause and this is a filter query, create a from clause. Otherwise, throw
// an exception because non-filter queries must have a FROM clause.
if (#f == null) {
if (filter) {
#f = #([FROM,"{filter-implied FROM}"]);
}
else
throw new SemanticException("FROM expected (non-filter queries must contain a FROM clause)");
}
which clearly states there are some HQL queries having no FROM clause, but that's acceptable if that's a filter query. Now again, I am not an expert in HQL/Hibernate, but I believe a filter query is not a full query but something you define using session.createFilter (see How do I turn item ordering HQL into a filter query?), so that makes me think there's no way to omit the FROM clause.
I'm use fake table with one row for example MyDual.
select case when exists(blah) then 1 else 0 end as conditionTrue from MyDual
According to http://docs.jboss.org/hibernate/core/3.3/reference/en/html/queryhql.html#queryhql-expressions it looks like they support both case and exists statements.

Use of CASE in Update query

I have following SQL (Using MS SQL)
UPDATE AR_Slots
SET Running = #Running,
StopSignal = #StopSignal,
WHERE (SlotId = #SlotId)
I want to set a field called RunListID to 0 if Running parameter (boolean) is true otherwise I don't want to change the value at all.
What is the "correct" way of doing this?
Thanks,
Stefan
Try something like
UPDATE AR_Slots
SET RunListID =
CASE
WHEN <your check value>
THEN 0
ELSE RunListID
END,
<your other sets>
WHERE <your criteria>