Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Can we use arrays for INSERT?

Author  Topic 

sd9sd
Starting Member

4 Posts

Posted - 2008-04-01 : 04:12:55
Hi,

I'm a complete newbie to SQL, and I need to know if there's an easy way to do this....I'm working with PHP, and it uses the following SQL statement:

$query = mysql_query("INSERT INTO `databaseName_tags`.`data` (`order` ,`name` ,`rating` ,`info` ,`comment` ,`comment2`)
VALUES ('0','abc','5','helllo','great work','woohooo')");

But what I'd prefer to do, is to have two arrays like so:

$cols = array("order" ,"name" ,"rating" ,"info" ,"comment","comment2");
$data = array("0","abc","5","helllo","great work","woohooo");
$query = mysql_query("INSERT INTO `databaseName_tags`.`data` ($cols) VALUES ($data)");

Is it possible to do it something like what is given above? It'd make the program so much more flexible....

nr
SQLTeam MVY

12543 Posts

Posted - 2008-04-01 : 05:45:13
Are you using sql server or mysql?

The answer to the question is no.
There are ways of dealing with arrays but not like you are trying to do.

Would be better to call a stored procedure to do the insert anyway rather than binding the application to the database structure.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

sd9sd
Starting Member

4 Posts

Posted - 2008-04-01 : 06:19:51
hey, thanks for replying!

I'm using mySQL (didn't know it's different from SQL server).
I didn't understand what you meant by calling a stored procedure.
And what do you mean by binding the application to the database structure? What application are you referring to? I don't understand these terms.....
Go to Top of Page

elancaster
A very urgent SQL Yakette

1208 Posts

Posted - 2008-04-01 : 06:29:57
quote:
Originally posted by sd9sd

hey, thanks for replying!

I'm using mySQL (didn't know it's different from SQL server).
I didn't understand what you meant by calling a stored procedure.
And what do you mean by binding the application to the database structure? What application are you referring to? I don't understand these terms.....



mySQL is a completely different product actually, and this forum is specifically for Microsoft SQL Server. I'm afraid to say I think you'd be better looking for a mySQL forum for help

Em
Go to Top of Page

sd9sd
Starting Member

4 Posts

Posted - 2008-04-01 : 06:38:08
very well....sorry for trespassing ;)
And thanks :)
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2008-04-01 : 06:38:37
>> And what do you mean by binding the application to the database structure? What application are you referring to? I don't understand these terms.....

I mean that you are including references to the database tables in your application. It means that if you change the database structure (e.g. splitting a table into two or combining tables for performance) then you have to change the applciation. ie the application is bound to the database structure.

If you have a stored procedure layer to aces the database then you can make any changes to the database and just have to change the stored procedures without affecting the applciation.

It also means that you can run the SPs without the application which is handy for testing and troubleshooting as well as finding whether the problem is with the application or database.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

sd9sd
Starting Member

4 Posts

Posted - 2008-04-01 : 06:52:30
@nr:
yeah, I was worried that it wouldn't be flexible...terms like 'stored procedures' etc are entirely new to me.....it does make sense, but can't understand any of it as yet.... :)

Thanks anyway...great forum!
I'll have to go hunting for a mySQL forum now :) adios!
Go to Top of Page
   

- Advertisement -