sql

Usage of nested using in C# and SQL Server

This thread is a continuation of http://stackoverflow.com/questions/2220422/is-there-a-reason-to-check-for-null-inside-multiple-using-clausule-in-c I've noticed that resharper lets me define using without opening any opening/closing bracket like in the method below (but then i can't use defined vars later on if the brackets ain't there,...

What's the best way to dedupe a table?

I've seen a couple of solutions for this, but I'm wondering what the best and most efficient way is to de-dupe a table. You can use code (SQL, etc.) to illustrate your point, but I'm just looking for basic algorithms. I assumed there would already be a question about this on SO, but I wasn't able to find one, so if it already exists just...

Create Insert Trigger for SQLAgentMail

Is it possible to create a trigger the will use SQLAgentMail and email an operator? ...

Executing a dynamic sql statement into a SYS_REFCURSOR

Hi all, is it possible to execute a dynamic piece of sql within plsql and return the results into a sys_refcursor? I have pasted my attempt soo far, but dosnt seam to be working, this is the error im getting throught my java app ORA-01006: bind variable does not exist ORA-06512: at "LIVEFIS.ERC_REPORT_PK", line 116 ORA-06512...

SQL query to return columns and values which match part of a where condition

Hi all, I am trying to find out if there's a good way to get all the column names, and values for a particular row, where a part of a condition is met. That is, I want to know which fields within my huge nested AND and OR where condition, met which conditions, and their values. The catch is I am actually using the Dynamic LINQ API over...

MySQL Many-To-Many Query Problem

Hello! Here's my problem. I have a many-to-many table called 'user_has_personalities'. In my application, users can have many personalities, and a personality can belong to many users. The table has two integer columns, user_id and personality_id. What I need to do is get all users that have at least all of the personalities (a set of...

Find all related records

I have an Order table that has a LinkedOrderID field. I would like to build a query that finds all linked orders and returns them in the result set. select OrderID,LinkOrderID from [Order] where LinkOrderID is not null OrderID LinkOrderID 787016   787037 787037   787786 787786   871702 I would like a stored proc that returns the ...

Couple of basic SQL query questions

Hello, Basically I got a small event system going on, but I'm having a couple of strange SQL query problems. 1st one I need to find all peoples names which have signed up for all 3 events. I tried to do: SELECT name FROM users NATURAL JOIN events WHERE events.id = '4' AND events.id = '7' AND events.id = '8' But it returns zero row...

Import 60mb XML file to SQL

I have a 60mb XML file that has a list of products, approx 8k of them. I need to get all the products from this xml file to a SQL table. The xml file has a static name so i know what to look for. I guess i want to know about the process, what makes the most sense and least overhead. How?What? is the best way to do this? When do i pa...

Single SQL query to check if either table contains a row with column=x

I have 2 unrelated tables A and B which both have foreign key constraints on C. I need to run an sql query that determines if either A or B contain a given id of C. My first approach was to use the union all but A and B are not related thus it will not work. Any ideas? ...

SQL to extract matlab date from postgres db

I'd like to construct a query to "convert" a postgresql datetime to a matlab datenum. Experience with other DBs has shown me that converting the date on the DB side is much faster than doing it in matlab. Matlab stores dates as number of days (including fractions) since an arbitrary epoch of a gregorian, non-existent date of 00-00-0000...

The best way to manage database changes

What is the best way to manage database changes? I need to have a solutions regardless the database client's language. Also I'd like to be able to use specific database features in those changes such as stored procedures, triggers and so on. ...

Select only newest records from table and make this FAST, how?

Good day, I have a question I'm struggling with a lot, hope somebody already found a clever solution to this (I use MySQL). I have table like this: Table `log` ---------- id inserted message user_id My goal is to select last inserted record for user and make this fast. Log table is huge (around 900k records), so my first approach was...

City belongsThroughCountyTo Province association, how to simplify the code?

Tables: Province hasMany County, County belongsTo Province, County hasMany City, City belongsTo County So basically something like: City belongsThroughCountyTo Province Situation: In a search form I have a select drop down menu with provinces. The "code": When I list the results, I first get ids of counties that belong to the speci...

Selecting products that haven't been made in 2 years

I'm trying to get the products that havn't been made in the last 2 years. I'm not that great with SQL but here's what i've started with and it doesn't work. Lets say for this example that my schema looks like this prod_id, date_created, num_units_created. I'll take any advice i can get. select id, (select date from table ...

Subsums in SQL Server 2000

Is there any nice way to get subsums in one query for data like this in sql server 2000? Input: Date Value 2008-06-20 10 2008-08-20 20 2008-10-05 5 2008-10-09 30 Desired output: 10 --sum of 1st value 30 --sum of 1st and 2nd values.. 35 65 ...

SQL: How do I refer to the result of a previous query?

Suppose I have a SQL query that looks like this: SELECT fName from employees where ssn=123456789; Suppose I want to follow the previous query with another one: SELECT fName from records WHERE ssn=123456789; SELECT lName from records WHERE fName=(the result of the previous query) What do I put in for (the result of the previous query)...

cities and distance by latitute-longitude

I have a table with: city latitude longitude And I need a sql query to know all cities are 100 miles from new york. Could anyone help me before I go crazy ? ...

NHibernate linq - Use lambda expression in place of formula attribute on mapping.

NHibernate has an attribute on the property element in the mapping config named "formula" that allows the injections of sql to "calculate" a property. The issue I have is the formula using sql syntax directly. Is there a way to have nhibernate.linq to use a lambda expression instead of using the formula property. I have the following: ...

SQL query to transform this data dimension..

Is there any simple SQL query to convert this below data Date - Item - cost 10/31/2009 - a - 1 10/31/2009 - b - 2 10/31/2009 - c - 3 10/31/2009 - d - 4 11/30/2009 - a - 5 11/30/2009 - b - 6 11/30/2009 - c - 7 11/30/2009 - d - 8 Into below report format without any application logic/store procedure/reportin...