update table column with Cursor value - sql

Hello I have a problem with cursors here I have this code in my PL/SQL
update CAND_ORDER SET SOURCE_SITE_ID = i.SITE_ID WHERE CAND_ORDER_ID IN
(select CAND_ORDER_ID from CAND_ORDER where SOURCE_SITE_ID is null and SP_TRIAL_MATERIAL_ID in
(select SP_TRIAL_MATERIAL_ID from SP_TRIAL_MATERIAL where SP_RESEARCH_STATION_ID_SOURCE IN
(select SP_RESEARCH_STATION_ID from SP_RESEARCH_STATION where SP_RESEARCH_STATION.CODE IN
(SELECT SITE_CODE from SITE WHERE SITE.SITE_CODE=i.SITE_CODE))));
it gives an error saying:
PLS-00302: component 'SITE_ID' must be declared
ORA-06550: line 17, column 44:
PL/SQL: ORA-00904: "I"."SITE_ID": invalid identifier
which I totally do not understand would you please help pointing what is the problem

I think that the error was in the cursor itself as SITE_ID was not added in the cursor as column. I added it and the problem was solved.

Related

How to drop a column starting with a Digit in Netezza

Due to some error while uploading data, extra columns got created and one of the column names became 84. Trying to remove that column but getting following error:
org.jkiss.dbeaver.model.sql.DBSQLException: SQL Error [1100] [HY000]: ERROR: 'ALTER TABLE XXX.XXXXX
DROP 84'
error ^ found "84" (at char 44) expecting an identifier, identifiers must begin with a letter
You can find examples of how to handle identifiers that does does not begin with a letter. You can wrap the identifier in double quotes
https://www.ibm.com/support/knowledgecenter/SSULQD_7.2.1/com.ibm.nz.dbu.doc/c_dbuser_handle_sql_identifiers.html

sql Error in derby - ERROR 42X01: Syntax error: Encountered "KEY"

following query gives error like ERROR 42X01: Syntax error: Encountered "KEY" at line 1, column 48.
I am not able to understand what is the exact issue. column KEY is exist and datatype is integer.
Insert into UOM_TYPE
(UNITS_OF_MEASURE_NO,TYPE,KEY,CREATED_BY,CREATED_DATE,MODIFIED_BY,MODIFIED_DATE,VERSION)
values
(79,'Clinical Property',32,'JRL',DATE('2007-05-04'),'JRL',DATE('2007-05-04'),2);
Please help me to resolve the issue
KEY is a Keyword in derby db. you have to escape it :
Insert into UOM_TYPE
(UNITS_OF_MEASURE_NO,TYPE,"KEY",CREATED_BY,CREATED_DATE,MODIFIED_BY,MODIFIED_DATE,VERSION)
values
(79,'Clinical Property',32,'JRL',DATE('2007-05-04'),'JRL',DATE('2007-05-04'),2);

Setting a varchar column length with a variable or a subquery

I have a table that is created by an import of an Excel file. As is the case, all text fields are imported as nvarchar(255). I'd rather do the bulk import then manipulate the table afterwards. I know SSIS allows me to set data types and sizes via data mapping, but that doesn't seem to work consistently for me. Maybe I'm not holding my mouth right...
Anyway, I want to change the varchar length definitions to the max length of the data in each column. Rather than run a statement to check the max length as a literal...
select max(len(rtrim(FIELD))) from TABLE$
...I want to do it in code. So I hit on this idea:
declare #Var int
set #Var = (select max(len(rtrim(FIELD))) from TABLE$)
alter table dbo.TABLE$ alter column FIELD varchar(#Var)
Lines one and two work fine, but it gives me an error when executing the third line:
Msg 102, Level 15, State 1, Line 5
Incorrect syntax near '#Var'.
So I tried this, thinking it would be a more compact solution...
alter table dbo.TABLE$
alter column FIELD varchar(select max(len(rtrim(FIELD))) from TABLE$)
...but it wasn't. I got these errors:
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'select'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near ')'.
So my question is kind of a two-parter. First, why won't these methods work, and second, what would work--short of finding the mas length of each column, then setting the varchar length with a literal?
Thanks in advance.
Dynamic SQL would work for what you're trying to do. Whether it's the right tool for the job or the right thing to do is another story. Give this a shot:
DECLARE #cmd NVARCHAR(4000)
SET #cmd = 'ALTER TABLE dbo.Table$ ALTER COLUMN FIELD VARCHAR(' + CAST((SELECT MAX(LEN(RTRIM(FIELD))) FROM dbo.TABLE$) AS NVARCHAR(10)) + ')'
EXEC(#cmd)
That query parses but I didn't try to run it. Be careful using Dynamic SQL though. It can get you into trouble if you start using it everywhere. Further reading:
http://sqlmag.com/database-performance-tuning/don-t-fear-dynamic-sql
http://www.sommarskog.se/dynamic_sql.html

Invalid column name '''

INSERT INTO #Prefix_PCAC_temp select rtrim(ltrim(replace(#str1,"'",null)))
in the above line it is showing error invalid column name '''.PLZ provide me any solution.
Use this to prevent incorrect syntax error:
INSERT INTO #Prefix_PCAC_temp select rtrim(ltrim(replace(#str1,'''',null)))
However, if there is ' symbol in your #str1 variable, the value will become NULL. Most likely you want this:
INSERT INTO #Prefix_PCAC_temp select rtrim(ltrim(replace(#str1,'''','')))
The query above eliminates ' symbols from #str1 variable and inserts the value into the table

Error when using openrowset for particular record

INSERT INTO OPENROWSET('MSDASQL', 'Driver=PostgreSQL Unicode;uid=postgres;Server=localhost;port=5432;database=data;pwd=xxx',
'select SanctionId,SchemeType,SchemeCode,CorrigendumStatus,AttendumStatus,yearofPlan,ReceivedDate from tesing WHERE SanctionId = ''-1'' ')
select SanctionId,SchemeType,SchemeCode,CorrigendumStatus,AttendumStatus,yearofPlan,ReceivedDate from testing where SanctionId=1103
While executing the above query, I am getting following error:
Msg 8152, Level 16, State 10, Line 1
String or binary data would be truncated.
The statement has been terminated.
Can anyone help me to resolve this?
You will have to check the source data against the target column definitions.
This happens when you try to insert, say, 100 characters into a varchar(50) column