I'm trying to construct a T-SQL statement with a WHERE clause determined by an input parameter. Something like:
SELECT * FROM table
WHERE id IN
CASE WHEN @param THEN
(1,2,4,5,8)
ELSE
(9,7,3)
END
I've tried all combination of moving the IN, CASE etc around that I can think of. Is this (or something like it) possible?
if I erase an element from an std::set and pass the key, not the iterator, and the key isn't in the set right now, will an exception be thrown? The thing is every second sentence in the MSDN documentation says: "this does blah blah, but it doesn't conform to the standard". So I need to know the standard behaviour. I just couldn't find it in the standard. Redirecting to the relevant clause will do as well. Thanks.
EXEC [dbo].[pr_cfgAddFact]
@SettingName = 'TransferBatch',
@RoleFK = SELECT TOP 1 rolepk FROM cfgRole WHERE cfgRole.Name = 'SuperAdmin'
Why would SQL complain about the SELECT clause here? I am trying to run the proc with the sub query getting the data.
Hi,
I try to use the SQL statement
SELECT * FROM table ORDER BY column
via an PDO-Object in PHP. Problem is, that I always get an error (Call to a member function fetchall() on a non-object - that means, the query did not return a PDO-object) when using the names of all columnname EXCEPT for ID. When I query
SELECT * FROM table ORDER BY ID
it works. ID is the PRIMARY INTEGER KEY, all other columns are TEXT or NUMERIC, neither of them would works with the ORDER BY clause.
Any ideas?
Whats wrong with this SQL? It should return results but returns nothing
SELECT `pid` FROM `products`
LEFT JOIN `prods_to_features`
ON (`ptf_pid` = `pid`)
WHERE (`ptf_id` = '66' OR `ptf_id` = '67')
AND (`ptf_id` = '76')
Is it not possible to have the 2nd where clause for the table that has been used in the left join?
I'd like to add a begin...rescue block to one of my controllers create method, in order to log better info and construct the correct error message to return to the client. Does the rescue in any way 'interrupt' the rollback process?
I'm assuming rails automatically does a rollback. When does it happen? Has it already happened by the time I get in the rescue clause?
I'm using mySQL on Dreamhost and I think they use innoDB.
I have a string which is fed into the query as IN clause,which looks like this ('ACT','INACT') which is one of the parameters to a function inside a package.when a call is made to the function from java,
it looks like this
call package.function(1,2,3,('ACT','INACT'),4,5).
When the package is called,i get error as wrong type of arguments.
It is taking the values inside brackets as different values delimited by strings
I can do this:
SELECT t2.value + sum(t3.value)
FROM tableA t2, tableB t3
WHERE t2.somekey = t3.somekey
GROUP BY t3.somekey
But how to do this?
UPDATE tableA t1
SET speed = (
SELECT t2.value + sum(t3.value)
FROM tableA t2, tableB t3
WHERE t2.somekey = t3.somekey
AND t1.somekey = t3.somekey
GROUP BY t3.somekey
)
;
MySQL says it's illegal since you can't specify target table t1 for update in FROM clause.
IEnumerable<T> collection;
void MyMethod(T toSearch)
{
foreach (T t in collection)
if (t == toSearch) {}
}
The if clause is never true.. is it because the Enumerator creates all the items instances on demand, so everytime a new Reference ?
foreach(var someDisposableObject in listOfDisposableObjects)
{
//some code
someDisposableObject.Dispose(); //current code contains something like this.
}
Is there safe way, like a using clause to use in this scenario?
Hello, I'm with an irritating problem. It might be something stupid, but I couldn't find out.
I'm using Linq to NHibernate, and I would like to count how many items are there in a repository. Here is a very simplified definition of my repository, with the code that matters:
public class Repository {
private ISession session;
/* ... */
public virtual IQueryable<Product> GetAll() {
return session.Linq<Product>();
}
}
All the relevant code in the end of the question.
Then, to count the items on my repository, I do something like:
var total = productRepository.GetAll().Count();
The problem is that total is 0. Always. However there are items in the repository. Furthermore, I can .Get(id) any of them.
My NHibernate log shows that the following query was executed:
SELECT count(*) as y0_ FROM [Product] this_ WHERE not (1=1)
That must be that "WHERE not (1=1)" clause the cause of this problem.
What can I do to be able .Count() the items in my repository?
Thanks!
EDIT: Actually the repository.GetAll() code is a little bit different... and that might change something! It is actually a generic repository for Entities. Some of the entities implement also the ILogicalDeletable interface (it contains a single bool property "IsDeleted"). Just before the "return" inside the GetAll() method I check if if the Entity I'm querying implements ILogicalDeletable.
public interface IRepository<TEntity, TId> where TEntity : Entity<TEntity, TId> {
IQueryable<TEntity> GetAll();
...
}
public abstract class Repository<TEntity, TId> : IRepository<TEntity, TId>
where TEntity : Entity<TEntity, TId>
{
public virtual IQueryable<TEntity> GetAll()
{
if (typeof (ILogicalDeletable).IsAssignableFrom(typeof (TEntity)))
{
return session.Linq<TEntity>()
.Where(x => (x as ILogicalDeletable).IsDeleted == false);
}
else
{
return session.Linq<TEntity>();
}
}
}
public interface ILogicalDeletable {
bool IsDeleted {get; set;}
}
public Product : Entity<Product, int>, ILogicalDeletable
{ ... }
public IProductRepository : IRepository<Product, int> {}
public ProductRepository : Repository<Product, int>, IProductRepository {}
Edit 2: actually the .GetAll() is always returning an empty result-set for entities that implement the ILogicalDeletable interface (ie, it ALWAYS add a WHERE NOT (1=1) clause.
I think Linq to NHibernate does not like the typecast.
Hey,everyone
I have a string 'some.file.name',I want to grab 'some.file'.
To do that,I need to find the last occurrence of '.' in a string.
My solution is :
declare @someStr varchar(20)
declare @reversedStr varchar(20)
declare @index int
set @someStr = '001.002.003'
set @reversedStr = reverse(@someStr)
set @index = len(@someStr) - charindex('.',@reversedStr)
select left(@someStr,@index)
Well,isn't it too complicated?I was just intented to using 'some.file' in a where-clause.
Anyone has a good idea?
Hello all,
I am just reading this article and I came across this:
Filter: Remove any functions in the
WHERE clause, don't include views in
your Transact-SQL code, may need
additional indexes.
If I do not use views, what are the alternatives? I mean, in my situation, I want to select some data from a table and then use a few other select queries to work on the subset of data from the first select query?
How can I do this efficiently?
Thanks all
How do I use the schema clauseon a bag object in Nhibernate mapping? I have an table that I need to sepcify the schema for and the nhibernate documentation talks about table schema but I can't find any further details...
I am trying to match a user inputted search term against two tables: posts and galleries. The problem is the union all clause isn't working. Is there something wrong with my code?
$query = mysql_query("
SELECT * FROM posts WHERE title LIKE '%$searchTerm%'
OR author LIKE '%$searchTerm%'
OR location LIKE '%$searchTerm%'
OR excerpt LIKE '%$searchTerm%'
OR content LIKE '%$searchTerm%'
UNION ALL
SELECT * FROM galleries WHERE title LIKE '%$searchTerm%'
");
i tried using this query:
"SELECT * FROM guests WHERE event_id=".$id." GROUP BY member_id;"
and I'm getting this error:
ERROR: column "guests.id" must appear in the GROUP BY clause or be used in an aggregate function
can anyone explain how i can work around this?
select @[email protected]('*')
for xml raw,type
Above statement will generate following alert:
Msg 6819, Level 16, State 3, Line 2
The FOR XML clause is not allowed in a ASSIGNMENT statement.
I have a select from (nothing to complex)
Select * from VIEW
This view has about 6000 records and about 40 columns. It comes from a Lotus Notes SQL database. So my ODBC drive is the LotusNotesSQL driver. The query takes about 30 seconds to execute. The company I worked for used EXCEL to run the query and write everything to the worksheet. Since I am assuming it writes everything cell by cell, it used to take up to 30 - 40 minutes to complete.
I then used MS access. I made a replica local table on Access to store the data. My first try was
INSERT INTO COLUMNS OF LOCAL TABLE
FROM (SELECT * FROM VIEW)
note that this is pseudocode. This ran successfully, but again took up to 20 - 30 minutes. Then I used VBA to loop through the data and insert it in manually (using an INSERT statement) for each seperate record. This took about 10 - 15 minutes. This has been my best case yet.
What i need to do after:
After i have the data, I need to filter through it by department. The thing is if I put a where clause in the SQL query (the time jumps from 30 seconds to execute the query, to about 10 minutes + the time to write to local table/excel). I dont know why. MAYBE because the columns are all text columns?
If we change some of the columns to integer, would that make it faster in terms of the where clause?
I am looking for suggestions on how to approach this. My boss has said we could employ some Java based solution. Will this help? I am not a java person but a c#, and maybe I'll convince them to use c# as well, but i am mainly looking for suggestions on how to cut down the time. I've already cut it down from 40 minutes to 10 minutes, but the want it under 2 minutes.
Just to recap:
Query takes about 30 seconds to exceute
Query takes about 15 - 40 minutes to be used locally in excel/acess
Need it under 2 minutes
Could use java based solution
You may suggest other solutions instead of java.
In what cases would a call to java.sql.ResultSetMetaData.isSearchable(int col) return false for an Oracle database? The documentation for the method doesn't really answer the question:
"Indicates whether the designated column can be used in a where clause."
I can think of only one case - when the column is the result of an aggregate function (in which case it would have to be part of a HAVING filter, not a WHERE filter).
Are there any other cases?
The first definition below produces the warning in the title when compiled with f# 3.0 and the warning level set to 5. The second definition compiles cleanly. I wondered if someone could please explain just what the compiler worries I might accidentally mutate, or how would splitting the expression with a let clause help avoid that. Many thanks.
let ticks_with_warning () : int64 =
System.DateTime.Now.Ticks
let ticks_clean () : int64 =
let t = System.DateTime.Now
t.Ticks
I have 3 tables:
shops, PRIMARY KEY cid,zbid
shop_items, PRIMARY KEY id
shop_inventory, PRIMARY KEY id
shops a is related to shop_items b by the following: a.cid=b.cid AND a.zbid=b.szbid
shops is not directly related to shop_inventory
shop_items b is related to shop_inventory c by the following: b.cid=c.cid AND b.id=c.iid
Now, I would like to run a query which returns a.* (all columns from shops). That would be:
SELECT a.* FROM shops a WHERE a.cid=1 AND a.zbid!=0
Note that the WHERE clause is necessary.
Next, I want to return the number of items in each shop:
SELECT a.*, COUNT(b.id) items FROM shops a
LEFT JOIN shop_items b ON b.cid=a.cid AND b.szbid=a.zbid
WHERE a.cid=1 GROUP BY b.szbid,b.cid
As you can see, I have added a GROUP BY clause for this to work.
Next, I want to return the average price of each item in the shop. This isn't too hard:
SELECT a.*, COUNT(b.id) items, AVG(COALESCE(b.price,0)) average_price FROM shops a
LEFT JOIN shop_items b ON b.cid=a.cid AND b.szbid=a.zbid
WHERE a.cid=1 GROUP BY b.szbid,b.cid
My next criteria is where it gets complicated. I also want to return the unique buyers for each shop. This can be done by querying shop_inventory c, getting the COUNT(DISTINCT c.zbid). Now remember how these tables are related; this should only be done for the rows in c which relate to an item in b which is owned by the respective shop, a.
I tried doing the following:
SELECT a.*, COUNT(b.id) items, AVG(COALESCE(b.price,0)) average_price, COUNT(DISTINCT c.zbid) FROM shops a
LEFT JOIN shop_items b ON b.cid=a.cid AND b.szbid=a.zbid
LEFT JOIN shop_inventory c ON c.cid=b.cid AND c.iid=b.id
WHERE a.cid=1 GROUP BY b.szbid,b.cid
However, this did not work as it messed up the items value. What is the proper way to achieve this result?
I also want to be able to return the total number of purchases made in each shop. This would be done by looking at shop_inventory c and adding up the c.quantity value for each shop. How would I add that in as well?
I need to loop through some elements in the page and then, for each one, if it had a class beginning with, for example, "C", do something.
$('#dialog li').each(function(){
if ($(this).hasClass("^C")){
//do something
}
}
It may sound silly, but what selector/method should I use on the if clause?
Thanks in advance.
Can anybody please spot my error, this should be a legal query in SQL shouldn't it??
Unknown column u.usr_auto_key in the ONclause
This is the database schema:
User: (usr_auto_key, name, etc...)
Setting: (set_auto_key, name etc..)
User_Setting: (usr_auto_key, set_auto_key, value)
And this is the query...
SELECT
`u`.`usr_auto_key` AS `u__usr_auto_key`,
`s`.`set_auto_key` AS `s__set_auto_key`,
`u2`.`usr_auto_key` AS `u2__usr_auto_key`,
`u2`.`set_auto_key` AS `u2__set_auto_key`,
`u2`.`value` AS `u2__value`
FROM `User` `u`, `Setting` `s`
LEFT JOIN `User_Setting` `u2` ON `u`.`usr_auto_key` = `u2`.`usr_auto_key`
WHERE (`s`.`sct_auto_key` = 1 AND `u`.`usr_auto_key` = 1 AND admin_property is null)
Writing some ruby code (not rails) and I need to handle something like this:
found 1 match
found 2 matches
I have rails installed so maybe I might be able to add a require clause at the top of the script, but does anyone know of a RUBY method that pluralizes strings? Is there a class I can require that can deal with this if the script isn't rails but I have rails installed?
Thanks in advance!