sql

Auto increment with a Unit Of Work

Context I'm building a persistence layer to abstract different types of databases that I'll be needing. On the relational part I have mySQL, Oracle and PostgreSQL. Let's take the following simplified MySQL tables: CREATE TABLE Contact ( ID varchar(15), NAME varchar(30) ); CREATE TABLE Address ( ID varchar(15), CONTACT_ID va...

How to Delete Duplicate Rows in SQL 2000?

I thought I had this figured out but it turns out I'm just deleting the first record. The following returns the duplicate rows. All have a count of 2. I just want to delete the first one for each duplicate record. select scorestudentid, scoreadvisor, scorecorrect, count(*) from scores where scoretestid = 3284 group by scorestudentid, s...

Is possible to reuse subqueries?

Hello, I'm having some problems trying to perform a query. I have two tables, one with elements information, and another one with records related with the elements of the first table. The idea is to get in the same row the element information plus several records information. Structure could be explain like this: table [ id, name ] [...

How do I strip multiple (optional) parts of a SQL string using .NET Regular Expressions?

I've been working on this for a few hours now and can't find any help on it. Basically, I'm trying to strip a SQL string into various parts (fields, from, where, having, groupBy, orderBy). I refuse to believe that I'm the first person to ever try to do this, so I'd like to ask for some advise from the StackOverflow community. :) To unde...

SQL Server 2008: Using Multiple dts Ranges to Build a Set of Dates

I'm trying to build a query for a medical database that counts the number of patients that were on at least one medication from a class of medications (the medications listed below in the FAST_MEDS CTE) and had either: 1) A diagnosis of myopathy (the list of diagnoses in the FAST_DX CTE) 2) A CPK lab value above 1000 (the lab value in th...

How to construct this query? (Ordering by COUNT() and joining with users table)

users table: id-user-other columns scores table: id-user_id-score-other columns They're are more than one rows for each user, but there's only two scores you can have. (0 or 1, == win or loss). So I want to output all the users ordered by the number of wins, and all the users ordered by the numbers of losses. I know how to do this...

How to use variables in Teradata SQL Macros

Hello I'm wanting to use variables inside my macro SQL on Teradata. I thought I could do something like the following: REPLACE MACRO DbName.MyMacro ( MacroNm VARCHAR(50) ) AS ( /* Variable to store last time the macro was run */ DECLARE V_LAST_RUN_DATE TIMESTAMP; /* Get last run date and store in V_LA...

Translate query to NHibernate

I am trying to learn NHibernate, and am having difficulty translating a SQL query into one using the criteria API. The data model has tables: Part (Id, Name, ...), Order (Id, PartId, Qty), Shipment (Id, PartId, Qty) For all the parts I want to find the total quantity ordered and the total quantity shipped. In SQL I have: select shipm...

"select * into table" Will it work for inserting data into existing table

I am trying to insert data from one of my existing table into another existing table. Is it possible to insert data into any existing table using select * into query. I think it can be done using union but in that case i need to record all data of my existing table into temporary table, then drop that table and finally than apply union...

Resultset comparison utilities

Is there any good tool which can compare the resultset for 2 queries and highlight the difference. This could be particularly useful when the queries are re-written for performance tuning and we want to be sure that the query produces same result. I was using Quest SQL optimizer to compare original and re-written queries but the tool sto...

complex sql which runs extremely slow when the query has order by clause

I have following complex query which I need to use. When I run it, it takes 30 to 40 seconds. But if I remove the order by clause, it takes 0.0317 sec to return the result, which is really fast compare to 30 sec or 40. select DISTINCT media.* , username from album as album , album_permission as permission , user a...

Returning multiple aggregate functions as rows

I need some help formulating a select statement. I need to select the total quantity shipped for each part with a distinct color. So the result should be a row with the color name and the total. Here's my schema: create table s ( sno char(5) not null, sname char(20) not null, status smallint, city char(15), pri...

SQLSTATE 08001 error

Our SQL Server 2008 got freeze.and not able to connect the databse and we restrted the server(windows 2008). Even we were not able to login windows(Restrted via HP ILO). Anybody face this issue and have a resolution? [165] ODBC Error: 0, Login timeout expired [SQLSTATE HYT00] [298] SQLServer Error: 258, Shared Memory Provider: Timeout ...

how to format a date in sql server 2008.

EDIT: How to format a date in sql sevrer with format dd-mm-yyyy to format yyyy-mm-dd. This format is not given in sql server but similar format is available yyyy/mm/dd Is it there any such function in sql server 2008 or 2005 ...

WHERE IN Query with two recordsets in Access VBA

Hi All, My first post here, so i hope this is the right area. I am currently trying to compare 2 recordsets, one of which has come from an Excel named range, and the other from a table in the Access database. The code for each is: Set existingUserIDs = db.OpenRecordset("SELECT Username FROM UserData") Set IDsToImport = exceldb.OpenReco...

SQL count NULL cells

Dear All, I have the following problem. I have a table in a db, with many columns. I can do different kind of select queries, to show, for example, for each record that satisfies a condition: all cells from columns with names ending in _t0 all cells from columns with names ending in _t1 ... To get the column lists to form the querie...

Why date comparison in sql is not working. Please help

I am trying to fetch some records from table but when i use OR instead of AND it returns me few records but not in other case. dates given exactly are present in table. What mistake i am doing ? select newsid,title,detail,hotnews from view_newsmaster where datefrom>=CONVERT(datetime, '4-22-2010',111) AND dateto<=CONVERT(datetime, '...

Exporting query results to a file on the fly

Hi all, I need to export the results of a query to a csv file and put the file on a network shared folder. Is it possible to achieve this within a stored procedure? If yes, comes yet another constraint: can I achieve this without sysadmin privileges, aka without using xp_cmdshell + BCP utility? If no to 2., does the caller have to h...

Issue while saving system.decimal values in a dataset

Hello I work on a data intensive module. I have a datacolumn inside a dataset which is bound to a grid. The set datacolumn property is as follows : Datatype : System.Decimal MaxLength : -1 DefaultValue : 0 With this properties i expect whenever i enter a number viz. 11001100011 to be saved as 11001100011.00 However internall...

LINQ to SQL for tables across databases. Or View?

I have a Message table and a User table. Both are in separate databases. There is a userID in the Message table that is used to join to the User table to find things like userName. How can I create this in LINQ to SQL? I can't seem to do a cross database join. Should I create a View in the database and use that instead? Will that work?...