Multiple returend values of SQL sub-query - sql

I am using PostgreSQL 9.1, I wrote the following SQL statement:
INSERT INTO "Tracking" VALUES
((SELECT "studentID" FROM "Student" WHERE "studentClass"='2'),false,4,false);
The issue is that the sub-query :
SELECT "studentID" FROM "Student" WHERE "studentClass"='2'
returned more than one value, and it is supposed to do that(I want to execute the main query per each returned value of sub-query), but by this way the Query will not be executed. Any Idea?

Try this:
INSERT INTO "Tracking"
SELECT "studentID",false,4,false
FROM "Student" WHERE "studentClass"='2'

Then use INSERT INTO... SELECT statement
INSERT INTO "Tracking"
SELECT "studentID" , false , 4 , false
FROM "Student"
WHERE "studentClass" = '2'
One thing to make sure about this statement is to make sure that table Tracking contains only 4 column or else you will getting number of columns mismatched with the supplied value. If for instance you have more than 4 columns, define the column name on the INSERT clause on which you want to save those values.

Related

Generate insert column based on select columns

I have a scenario, where 100's of select statements sql's are in one metadata table or some text file.
Need to insert all sql results into one specific table. (master table has col1, col2,col3 .... 200columns )
problem im facing(ORA-00947) is every select statement has different number of columns.
.. i need to generate INSERT PART.
CASE 1 : INSERT INTO (COL1,COL2,COL3) <<this select part comes from file/variable>>
CASE 2 : INSERT INTO (COL1) <<this select part comes from file/variable>>
CASE 3 : INSERT INTO (COL1) <<this select part comes from file/variable>>
have to figure out how many columns are in select part then generate INSERT part.
.
Thought of create as select but problem is some select statement has max(col) without alias so it will fail.
This is too long for a comment.
If you are storing SQL in a table, then you are constructing your query dynamically. So, update the table and list the columns that you want.
You could then construct the inserts as :
insert into master_table (<column list here>)
<select here>;
Both the select and column list would come from the table.
By far the easiest is to create a view for each SELECT statement. Then you can query the USER_TAB_COLUMNS view on the view name and get the column names.
Best regards,
Stew Ashton

Insert row to database based on form values not currently in database

I am using Access 2013 and I am trying to insert rows to a table but I don't want any duplicates. Basically if not exists in table enter the data to table. I have tried to using 'Not Exists' and 'Not in' and currently it still does not insert to table. Here is my code if I remove the where condition then it inserts to table but If I enter same record it duplicates. Here is my code:
INSERT INTO [UB-04s] ( consumer_id, prov_id, total_charges, [non-covered_chrgs], patient_name )
VALUES ([Forms]![frmHospitalEOR]![client_ID], [Forms]![frmHospitalEOR]![ID], Forms![frmHospitalEOR].[frmItemizedStmtTotals].Form.[TOTAL BILLED], Forms![frmHospitalEOR].[frmItemizedStmtTotals].Form.[TOTAL BILLED], [Forms]![frmHospitalEOR]![patient_name])
WHERE [Forms]![frmHospitalEOR]![ID]
NOT IN (SELECT DISTINCT prov_id FROM [UB-04s]);
You cannot use WHERE in this kind of SQL:
INSERT INTO tablename (fieldname) VALUES ('value');
You can add a constraint to the database, like a unique index, then the insert will fail with an error message. It is possible to have multiple NULL values for several rows, the unique index makes sure that rows with values are unique.
To avoid these kind of error messages you can build a procedure or use code to check data first, and then perform some action - like do the insert or cancel.
This select could be used to check data:
SELECT COUNT(*) FROM [UB-04s] WHERE prov_id = [Forms]![frmHospitalEOR]![ID]
It will return number of rows with the spesific value, if it is 0 then you are redy to run the insert.

Inserting into One Field Select statement Results

I am using Oracle databases. I have an sql table PS_Z_STAGE_TEST_JE that has three fields (EMPLID, LAST_NAME, FIRST_NAME).
I am trying to do a select statement that will pull many EMPLIDs from sql table:ps_vc_plan_mem and insert them into the EMPLID column while leaving the other two fields (LAST_NAME and FIRST_NAME) null.
Below is my SQL but it will say
Cannot insert null Value into LAST_NAME
when I try to run it.
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE (EMPLID)
SELECT DISTINCT(emplid)
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS','PNC-SO','PNC-ESPP');
The only obvious problem that I see with your query is the asterisk at the end:
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE(EMPLID)
SELECT DISTINCT emplid
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS', 'PNC-SO', 'PNC-ESPP');
Note that distinct is not a function. It is a modifier on select, so the parentheses don't mean anything.
The error message is exactly what it says it is. Your last_name column must be defined as not null. Therefore, you can't insert a null into it. Since you didn't define what to insert into the column in your insert, it tries to insert null by default and fails.
You must insert something into last name. I would suggest either a default string or an empty string if you can't get an actual last name to insert.
INSERT INTO sysadm.PS_Z_STAGE_TEST_JE (EMPLID, LAST_NAME)
SELECT DISTINCT(emplid), 'N/A'
FROM ps_vc_plan_mem
WHERE vc_plan_id IN ('PNC-RS','PNC-SO','PNC-ESPP');
Alternatively, you could alter your table so that last_name is nullable.
There is a asterisk at the end of your SQL statement. Please remove and retry.
If [sysadm].[PS_Z_STAGE_TEST_JE] table has a PK, you might want truncate the table before running the statement.

SQL How to insert null value

I want to insert data into one table from another table. In some of the columns I don't have data, so I want to set column to null. I don't know how I should do this?
This is the SQL:
INSERT INTO _21Appoint(
PCUCODE,PID,SEQ,
DATE_SERV,APDATE,
APTYPE,APDIAG,D_UPDATE,CID
) SELECT (
NULL,NULL,NULL,
treatment_date,appointment_date,
typeap_id,appointment_id,NULL,patient_id
) FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;
Your insert is essentially correct. Just don't put the column list in parentheses:
INSERT INTO _21Appoint
(PCUCODE,PID,SEQ,DATE_SERV,APDATE,APTYPE,APDIAG,D_UPDATE,CID)
SELECT NULL,NULL,NULL,treatment_date,appointment_date,typeap_id,appointment_id,NULL,patient_id
FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;
In Postgres (unlike other DBMS) putting a column list in parentheses makes the result a single "record", rather then individual columns. And therefore the select only returns a single column, not multiples and thus it doesn't match the column list for the insert
another option is to simply leave out the columns completely:
INSERT INTO _21Appoint
(DATE_SERV,APDATE,APTYPE,APDIAG,CID)
SELECT treatment_date,appointment_date,typeap_id,appointment_id,patient_id
FROM cmu_treatment,cmu_appointment
WHERE cmu_treatment.treatment_id LIKE cmu_appointment.treatment_id;

Postgres serial values insertion

From the Postgres documentation on INSERT, default keyword should auto increment columns declared as serial.But when I combine it with select statement, it throws me an error
syntax error at or near "DEFAULT"
Here is the insert statement
insert into abc (id,date,serialnumber) (DEFAULT,select (data.date,data.serialnumber) from data)
DEFAULT can only be as a "literal" for an INSERT statement in the VALUES clause. I cannot be used inside the column list of a SELECT statement even if that is used for an INSERT.
To apply the default value, simply leave out the column:
insert into abc (date,serialnumber)
select date, serialnumber
from data
For an example see here: http://sqlfiddle.com/#!12/d291a/1
Also: do not put a column list into parantheses. (a,b) is something different than a,b in Postgres. The first is a single record with two attributes, the second are two different columns.
See this SQLFiddle demo here: http://sqlfiddle.com/#!12/3a890/1 and note the difference between the two results.