insert-into

Conditional INSERT INTO statement in postgres

南笙酒味 提交于 2019-11-30 06:26:03
问题 I'm writing a booking procedure for a mock airline booking database and what I really want to do is something like this: IF EXISTS (SELECT * FROM LeadCustomer WHERE FirstName = 'John' AND Surname = 'Smith') THEN INSERT INTO LeadCustomer (Firstname, Surname, BillingAddress, email) VALUES ('John', 'Smith', '6 Brewery close, Buxton, Norfolk', cmp.testing@example.com'); But Postgres doesn't support IF statements without loading the PL/pgSQL extension. I was wondering if there was a way to do some

Insert Data into MSSQL DB using PHP

谁说我不能喝 提交于 2019-11-29 16:40:45
Hello there am trying to insert data into MSSQL using PHP. I have tried many times to figure out what the problem might be but i seem not to find it. Is there something am not getting right or missing? <?php //pull form fields into php variables $no = $_POST['no']; $name= $_POST['name']; $date = $_POST['date']; $leave = $_POST['leave']; $days= $_POST['days']; $empno= $_POST['empno']; //connect to sql $dbc = mssql_connect('Server-PC','user','password','database')or die('Error connecting to the SQL Server database.'); // Input into staff database $query = "INSERT INTO dbo.[CAGD$Leave Plan] ([No_

Enforce a foreign-key constraint to columns of same table

自古美人都是妖i 提交于 2019-11-29 04:03:16
How to enforce a constraint of foreign key on columns of same table in SQL while entering values in the following table: employee : empid number, manager number (must be an existing employee) instanceOfObject CREATE TABLE TABLE_NAME ( `empid_number` int ( 11) NOT NULL auto_increment, `employee` varchar ( 100) NOT NULL , `manager_number` int ( 11) NOT NULL , PRIMARY KEY (`empid_number`), CONSTRAINT `manager_references_employee` FOREIGN KEY (`manager_number`) REFERENCES (`empid_number`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 Hope it helps! Oracle call this a self-referential integrity constraint.

Move row from one table to another?

社会主义新天地 提交于 2019-11-28 23:18:59
I have two tables with the same column definitions. I need to move (not copy) a row from one table to another. Before I go off and use INSERT INTO/DELETE (in a transaction), is there a smarter way? SQL Server 2005 for SQL Server 2005 and up, try the OUTPUT Clause (Transact-SQL) clause: DELETE OldTable OUTPUT DELETED.col1, DELETED.col2... INTO NewTable WHERE ID=... Working example: DECLARE @OldTable table(col1 int, col2 varchar(5), col3 char(5), col4 datetime) DECLARE @NewTable table(col1 int, column2 varchar(5), col3 int , col_date char(23), extravalue int, othervalue varchar(5)) INSERT

Conditional INSERT INTO statement in postgres

前提是你 提交于 2019-11-28 19:08:48
I'm writing a booking procedure for a mock airline booking database and what I really want to do is something like this: IF EXISTS (SELECT * FROM LeadCustomer WHERE FirstName = 'John' AND Surname = 'Smith') THEN INSERT INTO LeadCustomer (Firstname, Surname, BillingAddress, email) VALUES ('John', 'Smith', '6 Brewery close, Buxton, Norfolk', cmp.testing@example.com'); But Postgres doesn't support IF statements without loading the PL/pgSQL extension. I was wondering if there was a way to do some equivalent of this or if there's just going to have to be some user interaction in this step?

Access Auto-Increment Value During INSERT INTO Statement

回眸只為那壹抹淺笑 提交于 2019-11-28 14:48:23
I am currently using MySQL. I have a table that has an auto_increment 'id' field, and an 'imgname' field containing a string that is the file name of an image. I need to generate the 'imgname' value using the auto_increment value that is create by an INSERT INTO statement. The problem is, I don't know this value until I can use mysql_insert_id, AFTER the insert query has run. I would like to know if it's possible to access this value DURING the insert query somehow and then use it to generate my string in the query statement. Thanks in advance. I would keep the id and imgname independent of

SQL Insert Into Temp Table in both If and Else Blocks

安稳与你 提交于 2019-11-28 11:01:52
I'm trying to populate a temp table based on the result of a condition in SQL 2005. The temp table will have the same structure either way, but will be populated using a different query depending on the condition. The simplified example script below fails in syntax checking of the ELSE block INSERT INTO with the error of: There is already an object named '#MyTestTable' in the database. DECLARE @Id int SET @Id = 1 IF OBJECT_ID('tempdb..#MyTestTable') IS NOT NULL DROP TABLE #MyTestTable IF (@Id = 2) BEGIN SELECT 'ABC' AS Letters INTO #MyTestTable; END ELSE BEGIN SELECT 'XYZ' AS Letters INTO

Insert Data into MSSQL DB using PHP

只谈情不闲聊 提交于 2019-11-28 10:36:53
问题 Hello there am trying to insert data into MSSQL using PHP. I have tried many times to figure out what the problem might be but i seem not to find it. Is there something am not getting right or missing? <?php //pull form fields into php variables $no = $_POST['no']; $name= $_POST['name']; $date = $_POST['date']; $leave = $_POST['leave']; $days= $_POST['days']; $empno= $_POST['empno']; //connect to sql $dbc = mssql_connect('Server-PC','user','password','database')or die('Error connecting to the

Access Auto-Increment Value During INSERT INTO Statement

半城伤御伤魂 提交于 2019-11-27 08:38:36
问题 I am currently using MySQL. I have a table that has an auto_increment 'id' field, and an 'imgname' field containing a string that is the file name of an image. I need to generate the 'imgname' value using the auto_increment value that is create by an INSERT INTO statement. The problem is, I don't know this value until I can use mysql_insert_id, AFTER the insert query has run. I would like to know if it's possible to access this value DURING the insert query somehow and then use it to generate

MySQL How do you INSERT INTO a table with a SELECT subquery returning multiple rows?

核能气质少年 提交于 2019-11-27 07:22:31
MySQL How do you INSERT INTO a table with a SELECT subquery returning multiple rows? INSERT INTO Results ( People, names, ) VALUES ( ( SELECT d.id FROM Names f JOIN People d ON d.id = f.id ), ( "Henry" ), ); I WANT to populate the new table with all results returning from this subquery. How do I do this without getting a ERROR 1242 (21000): Subquery returns more than 1 row INSERT INTO Results (People, names ) SELECT d.id, 'Henry' FROM Names f JOIN People d ON d.id = f.id Combine the static string Henry with your SELECT query. Umar Enesi Ibrahim INSERT INTO Results ( People, names, ) VALUES ( (