sql

SQL Sums in Rows Pulled out into Columns?

I have a search that is very slowly done by finding the primary records then looping in a thread to sum related transactions. I'm trying to make it work in a single statement, have come close but still have records which are alternating debit and credits. I can't work out how to pull the debit and credit rows up into columns so there is...

SQL query: Delete all records from the table except latest N?

Is it possible to build a single mysql query (without variables) to remove all records from the table, except latest N (sorted by id desc)? Something like this, only it doesn't work :) delete from table order by id ASC limit ((select count(*) from table ) - N) Thanks. ...

How to Prevent SQL Injection in Oracle SQLPlus?

Obviously if I am using JDBC/ODBC, I can use bind variables and prepared statements to prevent SQL injection. However, when data is passed to batch processes that end up invoking Oracle SQLPlus, is there a way to prevent SQL injection? For example: query.sql: select '&1' from dual; exit; If I call this script from SQLPlus thusly: $ ...

Complex join with nested group-by/having clause?

I ultimately need a list of "import" records that include "album" records which only have one "song" each. This is what I'm using now: select i.id, i.created_at from imports i where i.id in ( select a.import_id from albums a inner join songs s on a.id = s.album_id group by a.id having 1 = count(s.id) ); The nested sele...

Is there a way to stop SQL Express 2008 from Idling?

I am using SQL Express 2008 as a backend for a web application, the problem is the web application is used during business hours so sometimes during lunch or break time when there is no users logged in for a 20 minute period SQL express will kick into idle mode and free its cache. I am aware of this because it logs something like: S...

How can I manually add a compound association in entity framework?

This seems like a common case, but for whatever reason, I am stymied by EF's weak editor. I have two tables, like so: Table1 ( Column1 int, PK Column2 int, PK ) Table2 ( Column1 int, PK, FK Column2 int, PK, FK Column3 int, PK ) Now, the database (out of our control) does not have foreign key constraints. In this example, Table2 is s...

Convert date in TSQL

I am moving some data and I need to come up with a TSQL statement to convert dates currently in a datetime field to another database field with the varchar MM/yy format. This statement needs to work on both SQL Server 2k5 and also SQL Compact Edition 3.5 - so the answer needs to be "set" based and not include cursors etc that are not s...

"Good" Database Record Comparison tool?

I am a system administrator for a company that supports a CRM CMS (Salesforce). I don't like the built-in functionality so I generally manage data through CSV/XLS files for uploading and downloading, because I can write better queries and the like. One of my tasks is uploading Contacts and Accounts. Because of a lack of unique Identifie...

SQL Server Check for IsNull and for Zero

I have the following: set @SomeVariable = @AnotherVariable/isnull(@VariableEqualToZero,1) - 1 If @VariableEqualToZero is null it substitutes the 1. I need it to substitute 1 if @VariableEqualToZero = 0 as well. How do I do this? ...

How to randomly select rows in SQL??

In my db, I have a table "customerNames" which has two columns "Id" and "Name" and approx. 1,000 results. I am creating a functionality where I have to pick up the 5 customers randomly every time. Can anyone tell me how to create a query which will get random 5 rows (Id, and Name) every time when query is executed. edited I am using M...

Oracle query to get book-name?

table name:id col1:owner-name col2:book-id1 col3:book-id2 table name:book-name col1:id col2:name Can anyone get me a query to get following result owner-name,book1-name,book2-name using above 2 table ...

Description of columns in a DB2 table

Hi, How can we know the description of every column in a table(DB2) through SQL? My data base is DB2. Thanks in Advance ...

SQL query throws "not in aggregate function or group by clause" exception

I'm working on repairing the test suite for a project of ours, which is being tested through Hibernate/DBUnit. There are several test cases which all throw a similar exception from Hibernate, which looks something like this: java.sql.SQLException: Not in aggregate function or group by clause: org.hsqldb.Expression@109062e in statement ...

how to avoid sqlcmd blank line between Resultsets?

hello, refering my last question on extracting SQL stored procedures into .sql files (see here) I have another question: how to avoid or delete the sqlcmd blank line between Resultsets? Reason (see MSDN) When multiple results are returned, sqlcmd prints a blank line between each result set in a batch. This means that stored procedures...

What's the best way to write "nulls" to SQL database when presenting options in combobox?

Hi Guys, I'm writing a front end to a database, presenting options as comboboxes. And I need to be able to return nulls to the database. I have a solution at the moment which involves 'fudging' a record into the query to populate the Datatable, then detecting the selection and hard coding null into my update statement if this item has...

How to check if a temporary table is existing in Database.

Hi, I am actually dropping a temporary table in Database. I have to delete only if it exists in the Database. Please let me know how to check if a table exists in Data base. ...

Select COUNT(*) of subquery without running it twice

I've got a procedure to return a result set which is limited by page number and some other stuff. As an OUTPUT parameter I need to return a total amount of selected rows according to the parameters except the page number. So I have something like that: WITH SelectedItems AS (SELECT Id, Row1, Row2, ROW_NUMBER() OVER (ORDER BY Row1) AS Po...

Is primary key always clustered?

Hi, Please clear my doubt about this, In SQL Server (2000 and above) is primary key automatically cluster indexed or do we have choice to have non-clustered index on primary key? ...

What's faster, SELECT DISTINCT or GROUP BY in MySQL?

If I have a table CREATE TABLE users ( id int(10) unsigned NOT NULL auto_increment, name varchar(255) NOT NULL, profession varchar(255) NOT NULL, employer varchar(255) NOT NULL, PRIMARY KEY (id) ) and I want to get all unique values of profession field, what would be faster (or recommended): SELECT DISTINCT u.profession FR...

Parsing nested blocks in SQL using C#

i want to perform analysis on SQL code. In this i would like to separate the blocks in SQL code. for example, in the code below for condition1 loop stmt1; for condition2 loop stmt2; end loop; end loop; i want to separate these two blocks and create a data structure which will contain outer loop block and will also have inner loop bloc...