insert-into

Inserting strings into an SQL table depending on what results a query returns

喜你入骨 提交于 2019-12-12 00:45:50
问题 I have a handful of queries that I'm using to test whether or not database results from tests that I run don't have anything obviously broken and buggy in them. One of the queries has this basic form: SELECT * FROM Table WHERE Column = '' Its checking to make sure that one particular field is not blank. I want to keep track of these tests being run and when they pass or fail. Is there a way that I can write a query so that if that query brings back any results then it writes a string to

How to SELECT the result of RETURNING

别等时光非礼了梦想. 提交于 2019-12-11 15:38:58
问题 How to put the result of a RETURNING clause into a SELECT in PostgreSQL? I.e., I would like the SELECT statement to return whatever RETURNING returns. I have tried the following two syntaxes, none of them seem to work: (1) SELECT (INSERT INTO ... RETURNING *) (2) SELECT * FROM (INSERT INTO ... RETURNING *) ... You might ask, why I'm trying to do this, in spite of RETURNING already giving me the desired result? It's because I have a structure using two WITH clauses, and the insertion would be

INSERT INTO SELECT gives: Column name or number of supplied values does not match table definition

爱⌒轻易说出口 提交于 2019-12-11 08:00:39
问题 I'm getting this error message when executing the code below. Column name or number of supplied values does not match table definition. --DROP TABLE #UniqueBuildingUsageFkAndBuildingFk CREATE TABLE #UniqueUsageFkAndBuildingFk ( Building_fk INT NOT NULL ,Usage_fk INT NOT NULL DEFAULT(0) ); ;WITH UnknownUsageFkAndBuildingFkCTE AS ( SELECT DISTINCT mm.Building_fk, 0 AS Usage_fk FROM Customers.StandardizedRecord sr JOIN Customers.MidasMatch mm ON mm.CustomersBuildingNumberKey = sr

SQL INSERT sp_cursor Error

ε祈祈猫儿з 提交于 2019-12-11 06:59:00
问题 I have a pair of linked SQL servers: ServerA and ServerB. I want to write a simple INSERT INTO SELECT statement which will copy a row from ServerA's database to ServerB's database. ServerB's database was copied directly from ServerA's, and so they should have the exact same basic structure (same column names, etc.) The problem is that when I try to execute the following statement: INSERT INTO [ServerB].[data_collection].[dbo].[table1] SELECT * FROM [ServerA].[data_collection].[dbo].[table1] I

Insert into with union all and nextval doesn't work with duplicate values

喜你入骨 提交于 2019-12-11 06:28:05
问题 I want to insert 2 lines in a table within one insert into statement with Oracle SQL. This code works: insert into a_glw select tt.*, work_id_seq.nextval from (select 11111, 'one text', 12345, 'new text', NULL, 'some text', 'nice text', 'test', 'text', 'great text' from dual union all select 11111, 'one text', 12345, 'new text', NULL, 'some text', 'nice text', 'test', 'text', 'great text' from dual) tt; When I change the value test to text this code produces the error 00918. 00000 - "column

How to INSERT INTO…SELECT with ON DUPLICATE KEY

非 Y 不嫁゛ 提交于 2019-12-11 04:59:47
问题 I have two tables with identical structure. Table A contains all the current ads, Table B contains the archived ads. Column 1 (ad_id) is Primary Key, AI, INT. Table engine is MyISAM. I need to copy all the table A ads preceding a certain date to the archive, table B. My goal is that all fields except ad_id are duplicated, ad_id should get auto-incremented. Here is what I have attempted: INSERT INTO B`(`ad_id`, `ad_advertiser`, `ad_ln`, `ad_expire`) SELECT * FROM A WHERE YEAR( ad_expire ) <=

How does Oracle Insert Into work when order of values is not defined?

别来无恙 提交于 2019-12-11 01:12:32
问题 I came across some code that looks like this. I understand that it will return the auto-generated id, but what I don't understand is when I pass cursor data when I call this function, how does it identify what values are to be inserted in which columns when the column order is not defined? FUNCTION INSERT_ROW(DATA IN OWNER.TABLE%ROWTYPE) RETURN OWNER.TABLE.ID%TYPE IS l_ID OWNER.MY_TABLE.ID%TYPE; l_Data OWNER.MY_TABLE%ROWTYPE := DATA; BEGIN INSERT INTO OWNER.MY_TABLE VALUES l_Data RETURNING ID

INSERT INTO (SELECT & VALUES) TOGETHER

为君一笑 提交于 2019-12-10 17:43:43
问题 I'm trying to INSERT INTO a table, and I know 2 ways: Adding rows as values: INSERT INTO db_example.tab_example (id,name,surname,group) VALUES ('','Tom','Hanks','1'); or from another table: INSERT INTO db_example.tab_example (id,name,surname) SELECT ID,first_name,last_name FROM db_contacts.tab_mygroup; but what if I want to insert some values from another table (the second way), and some values manually like a default value (the first way). here's my try (it didn't work): INSERT INTO db

Insert New Row in Table 3 if combination of Col A and Col B in Table C Don't Exist

二次信任 提交于 2019-12-07 20:50:39
问题 I have a code that gets data from Tables 1 and 2, and then inserts new rows into Table 3. My problem is that the code adds records that already exist. How can I prevent duplicate errors from being inserted, when the combination of groupid and userid in Table C already exists? INSERT INTO mdl_groups_members (groupid,userid) SELECT l.mgroup AS moodle, r.id AS mdl_user FROM moodle AS l JOIN mdl_user AS r ON l.orders_id = r.id WHERE l.mgroup > 0 Here's the table before I ran the script: id

SQL Server: Select from two tables and insert into one

蓝咒 提交于 2019-12-06 12:19:19
问题 I have a stored procedure with two table variables (@temp and @temp2). How can I select the values from both temp tables (Both table variables contain one row) and insert them all in one table ? I tried the following but this didn't work and I got the error that the number of SELECT and INSERT statements does not match. DECLARE @temp AS TABLE ( colA datetime, colB nvarchar(1000), colC varchar(50) ) DECLARE @temp2 AS TABLE ( colD int ) ... INSERT INTO MyTable ( col1, col2, col3, col4 ) SELECT