7

I have this array

$REV = Array
(
    0 => 240,
    1 => 241,
    2 => 242,
    3 => 243,
    4 => 249
);

and i'm using this code bellow to insert for now, stored each array's element in a row with $id, $userID, Type and Date

if (!empty($REV)) {
    foreach ($REV as $val_rev) {
        $values_rev[] = "('" . $ID . "','REV','" . $val_rev . "','" . $IDUSER . "',GETDATE())";
    }
    $values_rev_insert = implode(',', $values_rev);

    $query_rev = "insert into dbo.CCLine (ID,CCType,CSID,IdUSer,DateCreated)values" . $values_rev_insert;
    mssql_query($query_rev);
}

But what i want is can use this stored procedure but i dont have idea how to make to insert in one time using the sp:

$stmt = mssql_init('sp_insertRev');
mssql_bind($stmt, '@ID', $ID, SQLINT4);
mssql_bind($stmt, '@CCType', 'REV', SQLVARCHAR);

The array does not work here

mssql_bind($stmt, '@CSID', $val_rev, SQLINT4);//An example 

mssql_bind($stmt, '@IdUSer', $IDUSER, SQLCHAR);
$result = mssql_execute($stmt);

How can i use this SP with the array

CREATE PROCEDURE [dbo].[sp_HCCInsert]
            @ID int
           ,@CCType varchar(10)
           ,@CSID varchar(10)
           ,@IdUSer char(15)

AS
BEGIN

    SET NOCOUNT ON;

    DECLARE @CCID as INT

    INSERT INTO [dbo].[CCLine]
           ([ID]
           ,[CCType]
           ,[CSID]
           ,[IdUSer]
           ,[DateCreated])
     VALUES
           (@ID
           ,@CCType
           ,@CSID
           ,@IdUSer
           ,GETDATE())


      SET @CCID = @@IDENTITY

      Select @CCID as CCID


END
Emilio Gort
  • 3,475
  • 3
  • 29
  • 44
  • Before thinking about how to store a comma separated list of values into a single column you should think about normalizing your data. A cell in a RDBMS usually represents an atomic value, not a set of values. – CodeZombie Jun 26 '13 at 23:26
  • hello @ZombieHunter, the database is normalized, above i´m inserting 4 columns for each element of array, the database is out of my hand, i cant change...what i want is take advantage of the sp, in this case the array only has 5 elements, but in normally production enviroment it can have 3 or 4 hundred elements..thanks for any sugestion or advice – Emilio Gort Jun 27 '13 at 00:32

3 Answers3

3

I've found solution to your problem in this post

It's all about passing the array as XML string which is passed to the procedure and in procedure it is used in INSERT SELECT statement with OPENXML function.

CREATE PROCEDURE [dbo].[sp_HCCInsert]
(
   @XMLDoc XML
)

Then use function OPENXML in MSSQL. You should read this topic. So pseudo code will look like

INSERT ... SELECT OPENXML(@XML...)

After you read it and fit to your needs just pass XML to procedure.

Some useful links about OPENXML

Moreover, I'd suggest using PDO because it has better abstract layer. I hope that it helped you.

Community
  • 1
  • 1
Robert
  • 19,800
  • 5
  • 55
  • 85
  • Hi robert, Do you have good experience with PDO MSSQL in PHP, I'm reading in the manual but it is still in EXPERIMENTAL time? – Emilio Gort Sep 19 '13 at 23:11
  • 1
    what's your problem? :) It's easy because of abstraction layer of PDO. You don't need to worry about in-mechanics of msssql. – Robert Sep 20 '13 at 11:43
1

I tend to use PDO instead of the mssql_* functions, you can still use dblib and you can bind that array quite simply. One thing though, you have 5 elements in that array but only 4 input variables to your stored procedure. This example will assume you only have 4 elements in your array.

$sql = "EXEC sp_HCCInsert ?, ?, ?, ?"; // SQL string with ? where params should go
$pdh = new PDO("dblib:host=somthing.somewhere\\INSTANCENAME;port=1433;dbname=MyDatabase;","username","password");
$sth = $pdh->prepare($sql); // Prepare query
$sth->execute($REV); // Execute and bind non-associative array

The only caution with using the PDO with dblib on PHP 5.3 or before, it goes and does a prefetch on execute. So if you execute a huge query and you want to loop through each record one at a time... sorry, you get the whole thing buffered first.

Also, you can use an associative array if you'd like

$REV = array(":ID" => 240, ":CCType" => 241, ":CSID" => 242, ":IdUSer" => 243);
$sql = "EXEC sp_HCCInsert :ID, :CCType, :CSID, :IdUSer"; // SQL string with named params
$pdh = new PDO("dblib:host=somthing.somewhere\\INSTANCENAME;port=1433;dbname=MyDatabase;","username","password");
$sth = $pdh->prepare($sql); // Prepare query
$sth->execute($REV); // Execute and bind associative array

The best part is, the execute method for the PDO::Statement escapes strings! Safety first you know.

Adam
  • 772
  • 3
  • 10
0
$sql  = "INSERT INTO table column(c1,c2) 
         VALUES('d1','d2'),('dd1','dd2'),('ddd1','ddd2')";// this is the basic sql command

$data = array('data1','data2','data3');
$data = '('.implode(', ',$data).')';
$sql  = "INSERT INTO table column(c1,c2) VALUES $data";

$data = array(array('1','2','3'),array('4','5','6'),array('7','8','9'));
$xa = array();
$a = 0;
foreach($data as $dt)
{
    $xa[$a] =   '('.implode(',',$dt).')';
    ++$a;
}
$data = '('.implode(',',$xa).')';
$sql  = "INSERT INTO table column(c1,c2) VALUES $data";
Igor Borisenko
  • 3,806
  • 3
  • 34
  • 49
  • thanks for the answer...but this dont answer my question, something like that is what i have, what i want is insert the array using the stored procedure, read the code below of array at beginning of the question – Emilio Gort Jul 02 '13 at 18:17