tags:

views:

62

answers:

6

Hi

Is it possible to write a update statement and not give the columns names. For example

UPDATE tbl VALUES('1','2','3','4') WHERE id = 1;

The number of values will always match the column count.

Thanks in advance.

EDIT

I don't know the column names only the number of columns.

I know i could delete the row and then do an insert be then the id(which is A_I) wont be the same.

Please help.

A: 

...know i could delete the row and then do an insert be then the id(which is A_I) wont be the same...

Assuming that your first column is an AUTO_INCREMENT NOT NULL.

INSERT into tbl VALUES(NULL, val1, val2);
Lekensteyn
My question says by not giving the column names. I don't know the column name so cannot specify it.
Ash Burlaczenko
Why wouldn't you give the column name? If a column gets moved, your application will be broken.
Lekensteyn
See me comment above to Albin.
Ash Burlaczenko
Ok, updated my post as you updated your question.
Lekensteyn
If i did that the id would increment but i want it to stay the same.
Ash Burlaczenko
Ok, replace NULL by your desired id then.
Lekensteyn
A_I's cannot have values insert into the column I don't think. Or can they?
Ash Burlaczenko
Why can't they? It just increments when there is no value given (a.k.a. NULL)
Lekensteyn
A: 

Even if it is possible, why would you want to do that? If you want to use the same query form for updates and inserts you can use "replace into tbl_name (column_names...) values (values...)"

Piotr Blasiak
+1  A: 

Don't ever try to do that.

That will make your code hard to read and impossible to maintain.
If you later change the columns of your table all your code will fall apart and since you don't have the column names in the code you will not be able to search for the changed columns and find where they are used.

Albin Sunnanbo
The number of values is generated depending of the number of columns in the table. All columns are varchar except for the first and this structure will never change.
Ash Burlaczenko
+2  A: 

No, it is not possible:

UPDATE Syntax

If you don't know the column names (which is rather strange), you could query the information schema:

INFORMATION_SCHEMA Tables

Mike
A: 

I'm not sure if it's possible in a simple SQL query. However, if you can create a stored procedure then you have options. I'm not going to research the options, but here's the example.

I know that Oracle has metadata that you can query, so you can get the column names of a table. (I think SQL server has something similar)

So, since you can query this metadata then you can loop through the column names, build a string with column names for your update statement and do an "execute" of that string

This has issues though: - security - SQL injection is possible since you are building a string to execute. Perhaps you could use bound variables or parameterize even the string you build and run - if you build a "hard-coded" UI, the moment the table changes and doesn't have default values for new columns or the columns are reordered, it will break. However, if you build the UI to read the metadata and then do the same thing to create the string to run then it should be fine.

+1  A: 

You can retrieve the list of columns using:

SELECT  COLUMN_NAME 
FROM    INFORMATION_SCHEMA.COLUMNS C 
WHERE   TABLE_NAME = 'MyTable'

That allows you to build a new SQL query which checks for a value in each column. It's best to do this in a client, like a C#, Python or perl script.

Andomar