sql

Select "where clause" evaluation order

In Sql Server 2005 when I have multiple parameters do I have the guarantee that the evaluation order will always be from left to right? Using an example: select a from table where c=1 and d=2 In this query if the "c=1" condition fails the "d=2" condition will never be evaluated? PS- "c" is an integer indexed column, d is a large ...

MS SQL Server: Check to see if a user can execute a stored procedure

How can you check to see if a user can execute a stored procedure in MS SQL server? I can see if the user has explicit execute permissions by connecting to the master database and executing: databasename..sp_helpprotect 'storedProcedureName', 'username' however if the user is a member of a role that has execute permissions sp_helprot...

TSQL: Howto add a char to a select statement.

I have the following statement SELECT id, descr from mytable which returns 1, 'Test1' 4, 'Test4' 6, 'Test6' 8, 'Test8' 22, 'Test22' Now I want the display to Add a sequential character to the first 4 results... 'A', 1, 'Test1' 'B', 4, 'Test4' 'C', 6, 'Test6' 'D', 8, 'Test8' '',22, 'Test22' Thoughts? Edit: Would prefer a SQL Ser...

[Oracle SQL] Passing different (multiple) cursors to the same For Loop

Edited for better clarification: Added 1/28/09: I over simplified the code to make it easy to explain, but the select statements are very long and complicated, and the second one is dependent on the first meaning after the first cursor is done and looped through and the inserts are created the second select actually looks at the fir...

How to make Emacs sql-mode recognize MySQL #-style comments?

I'm reading a bunch of MySQL files that use # (to end-of-line) comments, but my sql-mode doesn't support them. I found the syntax-table part of sql.el that defines /**/ and -- comments, but according to this, Emacs syntax tables support only 2 comment styles. Is there a way to add support for # comments in sql.el easily? ...

MySQL Insert Where query

What's wrong with this query: INSERT INTO Users( weight, desiredWeight ) VALUES ( 160, 145 ) WHERE id = 1; It works without the WHERE clause. I've seemed to have forgot my SQL.. ...

Using LINQ2Sql for Validate Users

Hi there im using Linq for check if two user fields correspond for an unique user register in SQL Table, for example UID : userID PIN : passID so fields have to be from a single user, i was trying this: public bool AutentificacionUsuario(string userID , string password passID) { USER _userID = _db.USER.FirstOrDefault(uid ...

Hibernate Performance Tweaks

In your experience what are some good Hibernate performance tweaks? I mean this in terms of Inserts/Updates and Querying. ...

Highest Performance Database Storage Mechanism

I need ideas to implement a (really) high performance in-memory Database/Storage Mechanism. In the range of storing 20,000+ objects, with each object updated every 5 or so seconds. I would like a FOSS solution. What is my best option? What are your experiences? I am working primarily in Java, but I need the datastore to have good perf...

T-SQL: How do I create a unique key that is case sensitive?

How do I create a unique constraint on a varchar field that is case sensitive (SQL Server 2005)? Currently my constraint looks like this: alter table MyTable add constraint UK_MyTable_MyUniqueKey unique nonclustered (MyCol) When I try to insert the following two values, I get a "Violation of UNIQUE KEY constraint..." error. insert i...

Generating a histogram from column values in a database

Let's say I have a database column 'grade' like this: |grade| | 1| | 2| | 1| | 3| | 4| | 5| Is there a non-trivial way in SQL to generate a histogram like this? |2,1,1,1,1,0| where 2 means the grade 1 occurs twice, the 1s mean grades {2..5} occur once and 0 means grade 6 does not occur at all. I don't mind if the...

(MySQL) OrderBy Field1=3, Field2

Hello I wish to order a table: Firstly by Field1=3 Then by Field2 DESC I know I can't write OrderBy Field1=3, Field2 DESC So how can I implement this?? TO CLARIFY: Let's say I have a table of books. I wish to list ALL the books in the table. I wish the books from 1990 to appear at the top, then the rest of the books in alphabetical...

Generate Delete Statement From Foreign Key Relationships in SQL 2008 ?

Is it possible via script/tool to generate a delete statement based on the tables fk relations. i.e. I have the table: DelMe(ID) and there are 30 tables with fk references to its ID that I need to delete first, is there some tool/script that I can run that will generate the 30 delete statements based on the FK relations for me ? (btw I...

SQL 2005 Linked Server Query Periodically Failing

We have a database running on SQL 2005. One of the store procedure looks up a user's email address from Active Directory using a linked server. The call to the linked server occurs in a database function. I'm able to call is successfully from my Asp.Net application the first time, but periodically after that, it fails with the followi...

SQL Stored-Proc using parameter for server name?

I have a stored procedure that gets all the non-system database names from a SQL Server: select name from MySQLServer.master.sys.databases where name not like ('master') and name not like ('tempdb') and name not like ('msdb') and name not like ('model') and name not like ('Admin') What I would like to do is pass the server name as a p...

What's the most efficient way to check the presence of a row in a table?

Say I want to check if a record in a MySQL table exists. I'd run a query, check the number of rows returned. If 0 rows do this, otherwise do that. SELECT * FROM table WHERE id=5 SELECT id FROM table WHERE id=5 Is there any difference at all between these two queries? Is effort spent in returning every column, or is effort spent in fil...

Setting status of other rows after INSERT

Hey, I have a field called STATUS and it is either 1 to show or 0 to hide. My code is below. I am using an edit in place editor with jQuery. Everytime you update it creates a new ROW which I want, but I want only the new one to have STATUS = 1 and the others to 0. Any ideas on how I would do that? <?php include "../../inc/config.inc...

Limiting returned record from SQL query in Oracle

One of apps I take care of in checking a Oracle DB table every x seconds to see if there are new data to process (other real-time app is populating it). Our new client business process forces our real-time up to populate this table with lots of records in a same time (say 10000) but just few times a day. Next time my app checks if there ...

FInd out the last write date in a SQL Server database

Folks, Assume you receive a disconnected backup of a SQL Server database (2005 or 2008) and you restore that to your SQL Server instance. Is there a way, is there a system catalog or something, to find out when the last write operation occured on that particular database? I'd like to be able to find out what day a particular database ...

OPENQUERY with a variable in a cursor

How can I return a OpenQuery in SQL Server including a variable to a cursor? DECLARE curMyCursor CURSOR FOR EXEC('SELECT * FROM OPENQUERY(SYBASE, ''SELECT * FROM MyTable WHERE MyPrimaryKey=''''' + @Variable + ''''''')') OPEN @ResultCrsr ...