sql

Get field names returned from any sql statement

I want to get a list of the field names returned from a sql statement. This can be done using a sql statement or some c# parsing code to parse the statement as a string. Can this be done easily without writing a complex parser? For example I may want to return name, field2, field3 from SELECT a.field1 as name, a.field2, b.field3 FR...

Using EXISTS with MySQL

I have this simple query that works on all other database systems, but fails with MySQL: UPDATE points p SET p.userid = 5224 WHERE p.userid = 2532 AND NOT EXISTS ( SELECT 1 FROM points q WHERE q.userid = 5224 AND q.game = p.game ) I get the following error message: #1093 - You can't specify target table 'p' for up...

Update table SQL query help

I have a database student(attribute - studentid). studentid is a varchar. Now I want to add 'P' at the end of all studentids. 12 -> 12P 234 -> 234P What will be the sql query for this? ...

What is the easiest way to see which join is responsible for an entry not being shown

I have this db with lots of tables and frequently I have to answer the question: Why is entry x not being shown? Often I have an idea which entry might be the reason, but more often its wild guessing and trying. This gets boring after some time. Is there a tool out there where I enter the joins and the key of the entry that I want to ...

Eval compare of two values to FALSE only when both are not null and don't Match

Where clause: I should test column against value in Proc, but if at least one of them is null it should be TRUE as a result. Only when BOTH NOT NULL (it's Integer values for an ID column in a table) should the result of the compare be FALSE. Now I have this: ... and nvl(nvl(b.account_id, account_id_), 0) = nvl(nvl(account_id_, b.accoun...

SQL Query problem

Consider two table.Employee and Project.Employee table has fields like eid,ename.Project table has fields like pid,pname.Now,since an employee can work on many projects and a project can be done by many employees, therefore,as evident,there is a many to many relationship b/w the two tables.Break the many to many,and create a new table ca...

SQL order by a column from another table

Hello, I have 3 tables: people, groups and memberships. Memberships is a join table between people and groups, and have 3 columns: personId, groupId and description (text). I want to select entries from the memberships table depending on a groupId but sorting the result by the names of people associated to the found memberships (name i...

Weird SQL Server behavior

What´s the name of the behavior in the statement below? Create table dbo.Foo(name varchar(10)) insert dbo.Foo (name) select 'Weird' union select 'SQL' union select 'Server' union select 'behavior' declare @title varchar(max) set @title = '' select @title = name + ' ' + @title from dbo.Foo select @title --returns 'Weird SQL Server b...

Database patterns for partitioning large hierarchical datasets

Are there any best-pratices/patterns or general advice for partitioning large amounts of hierarchical data? Think of, say, a database of all the people in a given country and tracking who has worked with who. Thinking of the "person" entities in isolation, if a lot of data were to be kept about each person then a natural approach seems...

How to implement a "hasChildren" SELECT statement in SQL?

Let's say I have the following table: ID | parentID | MoreStuff 1 | -1 | ... 2 | 1 | ... 3 | 1 | ... 4 | 2 | ... 5 | 1 | ... How can I generate an SQL SELECT statement to find out if a specific row has children? In other words, I want to know if ID 1 has children, which in this case it has 3...

PL/SQL...if inside a select?

I'm pretty new when it comes to PL/SQL, and I have to modify someone else's stored procedure. Is it possible to add an if statement in the middle of a select? The procedure I have tries to open a cursor and select a bunch of stuff from different tables into it (don't ask me why they didn't use a join) and return the cursor. The thing ...

PostgreSQL, update existing rows with pg_resotre

Hello. I need to sync two PostgreSQL databases (some tables from development db to production db) sometimes. So I came up with this script: [...] pg_dump -a -F tar -t table1 -t table2 -U user1 dbname1 | \ pg_restore -a -U user2 -d dbname2 [...] The problem is that this works just for newly added rows. When I edit non-PK column I get...

Combine two small queries (that group by different values) into one query

Please take a look at the following table (called response). It shows what response a respondent has given to a question and answer. questionid answerid respondentid 1 10 1 1 11 2 1 11 4 1 12 3 1 12 5 2 20 1 2 ...

Rails virtual attribute search or sql combined column search

Hello All! I have a user model with attributes 'first' and 'last' So for example User.first.first #=> "Charlie" User.first.last #=> "Brown" This User model also has a virtual attribute 'full_name' #user.rb def full_name [first,last].join(' ') end def full_name=(name) #don't know what to do with people w/ middle names split = name...

Save Sql Recordset into the csv file with well formatted?

I need to dump my sql query result into the text file. i have created the following query, DECLARE @cmd VARCHAR(2048) SET @cmd = 'OSQL -Slocalhost ' + ' -UCRN370 -PCRN370' + ' -Q"SELECT TOP 5 GageId FROM EwQMS370..msgages"' + ' -oc:\authors.csv' EXEC master..xp_cmdshell @cmd, NO_OUTPUT The ab...

How to get the dependency of jobs in SQL Server?

Hi, How can we find the job dependency in SQL server. Therotically basing on the success of first one job the other job begins. Thanks a lot in advance!! ...

Is there a way to search 2 or more fields at the same time?

I am looking into creating something that does this: SELECT * FROM (`tm_accounts`) WHERE CONCAT(`last_name`, `first_name`) LIKE '%o%' This, of course, doesn't work. I just want you to see the idea behind what I'm trying to emulate. last_name and first_name are two separate fields in the accounts table ...

Performance tuning with SQL Server

Hi, I was trying to contact the author of a book I am reading on SQL Server query performance, but it seems the e-mail address provided in the book does not exis any more. So I decided to ask the community. I am pasting the messasge I had written below. Thanks in advance. ====== I have bought your book (SQL Server 2008 Query Performanc...

How to retrieve employees belonging to more than one Owner using Oracle SQL

Hi, I have a situation in oracle where I have two tables - Table A and Table B. Table A has the following columns (aid, owner_id, app_id) and Table B has the following columns (bid, aid, emp_no) Table B has a foreign key (aid) back to Table A. Based on the above tables, I am trying to write a query where an emp_...

DAO, ORM and Queries

There is a need to update one field to the same value in a heap of records. Using the DAO/ORM structure, I would retrieve each parent object, loop through each child object, update it's field, and then save it. It would be faster to just write the SQL: update table set field = value where criteria = specified. How do I fit these things...