i have the following code:
$var1="bmx"
$newdata = implode(",", $dx);
$queryinsert = "INSERT INTO table VALUES ($var1,$newdata)";
but with this code i get some like
INSERT INTO table VALUES (bmx,black,white, green, yellow,grey)";
and i need to create a new row for each color, like
INSERT INTO table VALUES (bmx,white)";
INSERT INTO table VALUES (bmx,black)";etc...
i think i should use a foreach but i don't know how.
You need something like this.
Go to the php docs, you will find everithing there.
$var1="bmx"
foreach ($dx as &$value) {
$queryinsert = "INSERT INTO table VALUES ($var1,&$value)";
}
Related
So I learnt from here how to insert values into an array column:
INSERT INTO table
SELECT ARRAY("line1", "line2", "line3") as myArray
FROM source1;
And from here how to insert values into an struct column:
INSERT INTO table
SELECT NAMED_STRUCT('houseno','123','streetname','GoldStreet', 'town','London', 'postcode','W1a9JF') AS address
FROM source2;
Now I was trying to insert in the same way values in an array of structs. Which has got the following schema:
additionalattribute:array<struct<attribute_value:string,key:string,value:string>
I tried to extrapolate like this:
INSERT INTO table
ARRAY(NAMED_STRUCT('attribute_value','null','key','null','value','null')) as additionalattribute
FROM source2;
But it is not working. Does anyone know how to approach this issue?
you are missing the select statement after the table name. Demo
create table temp4
(
additionalattribute array<struct<attribute_value:string,key:string,value:string>>
);
INSERT INTO temp4 select
ARRAY(NAMED_STRUCT('attribute_value','null','key','null','value','null')) as additionalattribute
FROM (select '1' ) t;
This seems like a trivial question. And it is. But I have googled for over a day now, and still no answer:
I wish to do a bulk insert where for a column whose datatype is varchar(100), I wish to insert an empty string. Not Null but empty. For example for the table:
create table temp(columnName varchar(100))
I wish to insert an empty string as the value:
BULK INSERT sandbox..temp FROM
'file.txt' WITH ( FIELDTERMINATOR = '|#', ROWTERMINATOR = '|:' );
And the file contents would be row1|:row2|:|:|:. So it contains 4 rows where last two rows are intended to be empty string. But they get inserted as NULL.
This question is not the same as the duplicate marked question: In a column, I wish to have the capacity to insert both: NULL and also empty-string. The answer's provided does only one of them but not both.
Well instead of inserting empty string explicitly like this why not let your table column have a default value of empty string and in your bulk insert don't pass any values for those columns. Something like
create table temp(columnName varchar(100) default '')
I'm use local database in window phone .When I want insert data to table I'm use code as below:
ObjectTopicNews topic = new ObjectTopicNews
{
IDCat = IdCat,
Title=title,
Image=image
};
db.listData.InsertOnSubmit(topic);
db.SubmitChanges();
But I have a list data and I want insert all record of list to table and I don't know how to insert list to table,for example I want :
IList<ObjectTopicNews > list = getList();
db.listData.InsertOnSubmit(list );
db.SubmitChanges();
you will have iterate through list, retrieve object and insert
http://social.msdn.microsoft.com/Forums/vstudio/en-US/aaa25a0a-a7cb-4d44-9788-78f6d4b22355/how-to-iterate-through-list-or-ilist?forum=csharpgeneral
A SAMPLE table has only one column ID of type int, default null.
In Oracle when I do:
insert into SAMPLE (ID) values ('');
the new record is added with blank value. But in SQL Server 2008, when I run the same insert statement, the new record has the value of 0.
Is there a way to force SQL Server 2008 to default blank string to NULL instead of 0 (for numerical type of columns)?
Assuming that your INSERT statement is part of a stored procedure re-used in many places of your application (or, perhaps, is a batch always constructed by the same part of the client code) and that the inserted value is a number passed as a string argument, you could modify the INSERT like this:
INSERT INTO SAMPLE (ID) VALUES (NULLIF(#argument, ''));
Use NULL instead.
insert into SAMPLE (ID) values (NULL);
How about another idea - define an INSTEAD OF INSERT Trigger.
Despite the fact that you're trying to insert a string, with this the operation is "intercepted", empty string is replaced by NULL, and the insert succeeds.
If you define this trigger on your table, then you can continue to insert empty string as before, with no other changes.
Edit: As Martin Smith points out, this effectively is a comparison to 0 (the equivalent of empty string as an int) meaning you won't be able to store 0 in this table. I leave this answer here in case that's acceptable to your situation - either that or re-do all your queries!
CREATE TRIGGER EmptyStringTrigger
ON [SAMPLE]
INSTEAD OF INSERT
AS
BEGIN
INSERT INTO [SAMPLE](ID)
SELECT CASE
WHEN ID = '' THEN NULL
ELSE ID
END
FROM inserted
END
SQL Fiddle example
You can't insert a 'string' into a int column. Oracle must be just handling that for you.
Just try inserting NULL if that's what you need.
insert into SAMPLE (ID) values (NULL);
One more option
insert into SAMPLE (ID) values (DEFAULT)
I have a problem about inserting values into the table with sqlite.
supposing the table:
create table test
{
KeyName varchar(50) primary key,
KeyValue varchar (255)
};
I want to insert data like ('john', 'friend'), but I don't know whether the 'john' existed.
Currently I solve it:
using select * where KeyName = "john"
according the result from list 1, I use insert or update;
I'd like to know whether there is better solution?
thanks
you can use insert or replace which replaces the record if it already exists.
so you query be INSERT OR REPLACE INTO
check this link : http://www.sqlite.org/lang_conflict.html