A simple DBMS and SQL engine parser
hi guys;
i'm about to start an interesting c++ project described below...what i need is that you give me links ,advice and resources that could help me in my project...thanks for helping ...
here is the description:
Assignment:
In a nutshell, this assignment asks you to create a parse-tree for sample test SQL statements and actually create the underlying data structures to hold the data i.e. you are creating an extremely rudimentary SQL Engine (front-end parser) and a DBMS (back-end data structures). Our SQL Engine can be thought of as a bare-bone compiler. To test the SQL Engine and the DBMS, you will create a sample database. Finally (non-programming part), you will read up on the ODBC driver.
Note that a real compiler and a DBMS have functionality that is way beyond what I am going to ask you to implement. In fact, your DBMS software creation may disregard issues regarding efficiency ? those that couple the DBMS to the OS i.e. page buffering, blocking, concurrency, etc. Ours will be a data-centric view i.e. we will only focus on how best to save the user data. Treat this as DBMS-101 (It is more like DBMS-1.01).
Functionality:
Your SQL-Engine/DBMS software must offer the following capability-
Data: You must design data structures that will allow me to create tables and delete (drop) tables, insert tuples into tables, delete tuples from the tables and modify tuples values in tables possibly based on some criteria. Querying of course is the most important and frequently performed activity, but do NOT worry about joining tables or complicated queries. Data Serialization is of course implied by retrieval.
Meta-Data/Data Dictionary: In addition, I should be able to look up names of all tables that exist or the table definitions that exist and so on.
Future Requirements: I am being vague on this so that I may or may not add some small functionality later.
Software Components:
SQL Engine: Create a basic parser. Your parser should be able to parse the limited SQL statements that have been provided in Appendix A.
? Include the test cases you used for the parser. Software Architecture and Implementation documents should also be attached.
? Have the parser create parse trees for the sample SQL statements in accordance with your proposed grammar. The input could come from command-line or a stored file.
? Print the parse trees (In-order and Pre-order) for sample input to present proof of correctness.
? That completes the front-end of our SQL-engine.
DBMS: Propose a data structure, which will house the actual database. The back-end of the program will ?walk? the parse tree and create/fill in the data-structures in the back-end DBMS accordingly.
? Data Dictionary ? meta-data details. I.e. lists details about tables and fields.
? Data Definition ? creation of tables.
? Data Manipulation ? update/modify, storage and retrieval of records.
? Disregard issues regarding efficiency ? those that couple the DBMS to the OS i.e. page buffer, blocking, etc.
Database: Create a sample database to test your SQL/DBMS software. You must attach database schemas, tables with inserted values in your final submission.
I have no comments on the parsing of the SQL language. Without looking at Appendix A, I can't tell what you are being asked to do. However I do have a suggestion for a rather bogus back end storage that seems to fit the spirit of the assignment.
Create a directory named DB and in it keep files that are named things like
int_Employee_ID
string10_Employee_Name
where you follow the naming convention
DataType_TableName_FieldName
What this does for you is this:
To create a table just create files with the names and the datatypes that youwant.
To delete a table just delet files that have that table name as the secon parameter
The data dictionay is essentially a dump of the filenames in the directory.
Given that the data type in encoded in the file name it is easy to locate the ith element in any file for reading or writing.
Yes, it is bogus. It requires that you make dozens of disk reads to pick up all the pieces of a single record. Good thing efficiency was not listed as a requirement. You would NOT want this structure if you were looking for speed, or making sure that no tranaction gets only halfway done during a power outage.
But since the assignment is NOT about building databases but rather about parsing SQL statements, this layout gives you a fairly transparent way to represent your data and allows you to do nice bogus things like to create your initial data all with a simple text editor.
Since there are no joins to be requested of your system, all your SQL queries will essentially boil down into building filters that will run one or more of these lists and essentially create a list of indicies for records in a single table.
Similar Messages
-
Simple: dll and runtime engine
Do I need the LV runtime engine to be installed on the machine to access the functions from the .dll file built out from the LV project?
Solved!
Go to Solution.Simple: Yes. And you will need the "correct" version!
Norbert
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it. -
In which sequence are queries and sub-queries executed by the SQL Engine
In which sequence are queries and sub-queries executed by the SQL Engine?
Which is correct ?
a. primary query -> sub query -> sub sub query and so on
b. sub sub query -> sub query -> prime query
c. the whole query is interpreted at one time
d. there is no fixed sequence of interpretation, the query parser takes a decision on the flyIn which sequence are queries and sub-queries executed by the SQL Engine?
Which is correct ?
a. primary query -> sub query -> sub sub query and so on
b. sub sub query -> sub query -> prime query
c. the whole query is interpreted at one time
d. there is no fixed sequence of interpretation, the query parser takes a decision on the fly
Hi, below may help you understanding simple subquery & correlated subquery
refer :
http://sqlmag.com/t-sql/t-sql-starters-simple-and-correlated-subqueries
a) if it is correlated subquery
A correlated subquery is one that depends on a value in the outer query. In programming terms, you pass the subroutine an argument, then the subroutine evaluates the query and returns a result. You move on to the next record and repeat the process. The
net effect is that the subquery runs once for every row in the main query; this situation is inefficient.
A possible alternative is to rewrite the correlated subquery as a join. However,
some situations require a subquery. Eg- IN(with outer table refernce)
so main query -> subquery
b) if it is simple subquery
A subquery is a query that SQL Server must evaluate before it can process the main query, Eg- IN(with out outer table refernce)
so subquery-> main query
c) correlated query interpreted row by row with subquery, while subquery is interpreted at one time(outer table and inner table)
d) in most case it depends on the whether it is correlated subquery or simple subquery
Thanks
Saravana Kumar C -
Diff between oracle 8i sql Engine and oracle 9i sql Engine
hi,
1)Can anyone tell me difference between oracle8i sql engine and 9i sql engine.
2)Does oracle 9i uses the index defaultly if any primary key is there in table
Thanks
Rama> 1)Can anyone tell me difference between oracle8i
sql engine and 9i sql engine.
They are different versions.
If you have been expecting a list of differences and enhancements and new product features... forget it. It will take several pages to list. Rather refer to the 9i product feature sheet.
> 2)Does oracle 9i uses the index defaultly if any
primary key is there in table
Huh? Oracle will use an index if it determines that it costs less to use that index to get to the data, than not using an index. -
Database engine services and sql server replication failed in installation
hi im trying to setup sql server on my laptop but it says the 'database engine services' and 'sql server replication' have failed due to some specified module being missing can anyone help me out?
log file:
Overall summary:
Final result: Failed: see details below
Exit code (Decimal): -2147467259
Start time: 2015-03-17 10:47:47
End time: 2015-03-17 11:08:11
Requested action: Install
Setup completed with required actions for features.
Troubleshooting information for those features:
Next step for SQLEngine: Use the following information to resolve the error, uninstall this feature, and then run the setup process again.
Next step for Replication: Use the following information to resolve the error, uninstall this feature, and then run the setup process again.
Machine Properties:
Machine name: SHARI
Machine processor count: 2
OS version: Future Windows Version
OS service pack:
OS region: United States
OS language: English (United States)
OS architecture: x64
Process architecture: 32 Bit
OS clustered: No
Product features discovered:
Product Instance Instance ID
Feature Language
Edition Version Clustered
SQL Server 2008 SQLEXPRESS MSSQL10.SQLEXPRESS Database Engine Services
1033 Express Edition 10.1.2531.0 No
SQL Server 2008 SQLEXPRESS MSSQL10.SQLEXPRESS SQL Server Replication
1033 Express Edition 10.1.2531.0 No
SQL Server 2012
Management Tools - Basic 1033 Express Edition
11.0.2100.60 No
SQL Server 2012
LocalDB 1033
Express Edition 11.0.2100.60 No
Package properties:
Description: Microsoft SQL Server 2012 Service Pack 1
ProductName: SQL Server 2012
Type: RTM
Version: 11
SPLevel: 0
Installation location: c:\efa3a640a9b59ee651913f55719bf3b1\x86\setup\
Installation edition: Express
Product Update Status:
None discovered.
User Input Settings:
ACTION: Install
ADDCURRENTUSERASSQLADMIN: true
AGTSVCACCOUNT: NT AUTHORITY\NETWORK SERVICE
AGTSVCPASSWORD: *****
AGTSVCSTARTUPTYPE: Disabled
ASBACKUPDIR: Backup
ASCOLLATION: Latin1_General_CI_AS
ASCONFIGDIR: Config
ASDATADIR: Data
ASLOGDIR: Log
ASPROVIDERMSOLAP: 1
ASSERVERMODE: MULTIDIMENSIONAL
ASSVCACCOUNT: <empty>
ASSVCPASSWORD: <empty>
ASSVCSTARTUPTYPE: Automatic
ASSYSADMINACCOUNTS: <empty>
ASTEMPDIR: Temp
BROWSERSVCSTARTUPTYPE: Manual
CLTCTLRNAME: <empty>
CLTRESULTDIR: <empty>
CLTSTARTUPTYPE: 0
CLTSVCACCOUNT: <empty>
CLTSVCPASSWORD: <empty>
CLTWORKINGDIR: <empty>
COMMFABRICENCRYPTION: 0
COMMFABRICNETWORKLEVEL: 0
COMMFABRICPORT: 0
CONFIGURATIONFILE:
CTLRSTARTUPTYPE: 0
CTLRSVCACCOUNT: <empty>
CTLRSVCPASSWORD: <empty>
CTLRUSERS: <empty>
ENABLERANU: true
ENU: true
ERRORREPORTING: true
FEATURES: SQLENGINE, REPLICATION
FILESTREAMLEVEL: 0
FILESTREAMSHARENAME: <empty>
FTSVCACCOUNT: <empty>
FTSVCPASSWORD: <empty>
HELP: false
IACCEPTSQLSERVERLICENSETERMS: true
INDICATEPROGRESS: false
INSTALLSHAREDDIR: c:\Program Files\Microsoft SQL Server\
INSTALLSHAREDWOWDIR: c:\Program Files (x86)\Microsoft SQL Server\
INSTALLSQLDATADIR: <empty>
INSTANCEDIR: C:\Program Files (x86)\Microsoft SQL Server\
INSTANCEID: SHARIINSTANCE
INSTANCENAME: SHARIINSTANCE
ISSVCACCOUNT: NT AUTHORITY\Network Service
ISSVCPASSWORD: <empty>
ISSVCSTARTUPTYPE: Automatic
MATRIXCMBRICKCOMMPORT: 0
MATRIXCMSERVERNAME: <empty>
MATRIXNAME: <empty>
NPENABLED: 0
PID: *****
QUIET: false
QUIETSIMPLE: false
ROLE: AllFeatures_WithDefaults
RSINSTALLMODE: DefaultNativeMode
RSSHPINSTALLMODE: DefaultSharePointMode
RSSVCACCOUNT: <empty>
RSSVCPASSWORD: <empty>
RSSVCSTARTUPTYPE: Automatic
SAPWD: <empty>
SECURITYMODE: <empty>
SQLBACKUPDIR: <empty>
SQLCOLLATION: SQL_Latin1_General_CP1_CI_AS
SQLSVCACCOUNT: NT Service\MSSQL$SHARIINSTANCE
SQLSVCPASSWORD: <empty>
SQLSVCSTARTUPTYPE: Automatic
SQLSYSADMINACCOUNTS: SHARI\Shari Donald
SQLTEMPDBDIR: <empty>
SQLTEMPDBLOGDIR: <empty>
SQLUSERDBDIR: <empty>
SQLUSERDBLOGDIR: <empty>
SQMREPORTING: true
TCPENABLED: 0
UIMODE: AutoAdvance
UpdateEnabled: true
UpdateSource: MU
X86: true
Configuration file: C:\Program Files (x86)\Microsoft SQL Server\110\Setup Bootstrap\Log\20150317_104244\ConfigurationFile.ini
Detailed results:
Feature: Database Engine Services
Status: Failed: see logs for details
Reason for failure: An error occurred during the setup process of the feature.
Next Step: Use the following information to resolve the error, uninstall this feature, and then run the setup process again.
Component name: SQL Server Database Engine Services Instance Features
Component error code: 0x80004005
Error description: The specified module could not be found.
Error help link: http://go.microsoft.com/fwlink?LinkId=20476&ProdName=Microsoft+SQL+Server&EvtSrc=setup.rll&EvtID=50000&ProdVer=11.0.3128.0&EvtType=0xEC717F7D%400xC24842DB&EvtType=0xEC717F7D%400xC24842DB
Feature: SQL Server Replication
Status: Failed: see logs for details
Reason for failure: An error occurred for a dependency of the feature causing the setup process for the feature to fail.
Next Step: Use the following information to resolve the error, uninstall this feature, and then run the setup process again.
Component name: SQL Server Database Engine Services Instance Features
Component error code: 0x80004005
Error description: The specified module could not be found.
Error help link: http://go.microsoft.com/fwlink?LinkId=20476&ProdName=Microsoft+SQL+Server&EvtSrc=setup.rll&EvtID=50000&ProdVer=11.0.3128.0&EvtType=0xEC717F7D%400xC24842DB&EvtType=0xEC717F7D%400xC24842DB
Feature: SQL Browser
Status: Passed
Feature: SQL Writer
Status: Passed
Rules with failures:
Global rules:
Scenario specific rules:
Rules report file: C:\Program Files (x86)\Microsoft SQL Server\110\Setup Bootstrap\Log\20150317_104244\SystemConfigurationCheck_Report.htmHello,
As per the log details shared, installing version is SQL server express. Please go to control panel - add and remove programs and uninstall the
SQL Server Database Engine Services Instance Features, and also Uninstall all SQL Services.
Reboot and start the start the installation with RUN as Administrator.
Thank You.
Regards, Pradyothana DP. Please Mark This As Helpful if it helps to solve your issue. ========================================================== http://www.dbainhouse.blogspot.in/ -
How the SQL Query Parsing is processing inside SQL/PLSQL engine?
Hi all,
Can you explain how the SQL Query Parsing is processing inside SQL/PLSQL engine?
Thanks,
SankarSankar,
Oracle Database concepts - Chapter 24..
You will find the explanation required under the heading parsing.
http://download-west.oracle.com/docs/cd/B19306_01/server.102/b14220/sqlplsql.htm -
I ran SQL Server 2012 express setup as user with administrator privileges and still it's failing to install "Database Engine Service" and "SQL Server Replication".
Any and every ideas on how to resolve this issue is greatly appreciated.Three weeks passed and I am yet to find a fix for the the failed installation of the database engine on sql server 2012 express installation. I have tried various work-around including uninstalling, deleting all the directory paths, and reinstallation. Still
not able to successfully install sql server 2012 express.
I will greatly appreciate any and every contribution towards resolving this issue. Thanks in advance.
Here is the content of log file after the latest failed installation:
Overall summary:
Final result: Failed: see details below
Exit code (Decimal): -2061893607
Start time: 2013-12-23 14:42:45
End time: 2013-12-23 15:07:51
Requested action: Install
Setup completed with required actions for features.
Troubleshooting information for those features:
Next step for SQLEngine: Use the following information to resolve the error, uninstall this feature, and then run the setup process again.
Next step for Replication: Use the following information to resolve the error, uninstall this feature, and then run the setup process again.
Machine Properties:
Machine name: mymachine1
Machine processor count: 4
OS version: Windows 7
OS service pack: Service Pack 1
OS region: United States
OS language: English (United States)
OS architecture: x86
Process architecture: 32 Bit
OS clustered: No
Product features discovered:
Product Instance Instance ID
Feature Language
Edition Version Clustered
SQL Server 2012 SQLEXPRESS_GT MSSQL11.SQLEXPRESS_GT Database Engine Services
1033 Express Edition 11.0.2100.60 No
SQL Server 2012 SQLEXPRESS_GT MSSQL11.SQLEXPRESS_GT SQL Server Replication
1033 Express Edition 11.0.2100.60 No
SQL Server 2012
Management Tools - Basic 1033 Express Edition
11.0.2100.60 No
Package properties:
Description: Microsoft SQL Server 2012 Service Pack 1
ProductName: SQL Server 2012
Type: RTM
Version: 11
Installation location: C:\9b6607e524727c7fe1defd80\x86\setup\
Installation edition: Express
Slipstream: True
SP Level 1
Product Update Status:
Success: KB 2674319
Product Updates Selected for Installation:
Title: Service Pack 1
Knowledge Based Article: KB 2674319
Version: 11.1.3000.0
Architecture: x86
Language: 1033
Update Source: MU
User Input Settings:
ACTION: Install
ADDCURRENTUSERASSQLADMIN: true
AGTSVCACCOUNT: NT AUTHORITY\NETWORK SERVICE
AGTSVCPASSWORD: *****
AGTSVCSTARTUPTYPE: Disabled
ASBACKUPDIR: Backup
ASCOLLATION: Latin1_General_CI_AS
ASCONFIGDIR: Config
ASDATADIR: Data
ASLOGDIR: Log
ASPROVIDERMSOLAP: 1
ASSERVERMODE: MULTIDIMENSIONAL
ASSVCACCOUNT: <empty>
ASSVCPASSWORD: <empty>
ASSVCSTARTUPTYPE: Automatic
ASSYSADMINACCOUNTS: <empty>
ASTEMPDIR: Temp
BROWSERSVCSTARTUPTYPE: Disabled
CLTCTLRNAME: <empty>
CLTRESULTDIR: <empty>
CLTSTARTUPTYPE: 0
CLTSVCACCOUNT: <empty>
CLTSVCPASSWORD: <empty>
CLTWORKINGDIR: <empty>
COMMFABRICENCRYPTION: 0
COMMFABRICNETWORKLEVEL: 0
COMMFABRICPORT: 0
CONFIGURATIONFILE:
CTLRSTARTUPTYPE: 0
CTLRSVCACCOUNT: <empty>
CTLRSVCPASSWORD: <empty>
CTLRUSERS: <empty>
ENABLERANU: true
ENU: true
ERRORREPORTING: false
FEATURES: SQLENGINE, REPLICATION, CONN, BC, SDK, ADV_SSMS, SNAC_SDK
FILESTREAMLEVEL: 0
FILESTREAMSHARENAME: <empty>
FTSVCACCOUNT: <empty>
FTSVCPASSWORD: <empty>
HELP: false
IACCEPTSQLSERVERLICENSETERMS: true
INDICATEPROGRESS: false
INSTALLSHAREDDIR: c:\Program Files\Microsoft SQL Server\
INSTALLSHAREDWOWDIR: <empty>
INSTALLSQLDATADIR: <empty>
INSTANCEDIR: C:\Program Files\Microsoft SQL Server\
INSTANCEID: SQLEXPRESS
INSTANCENAME: SQLEXPRESS
ISSVCACCOUNT: NT AUTHORITY\Network Service
ISSVCPASSWORD: <empty>
ISSVCSTARTUPTYPE: Automatic
MATRIXCMBRICKCOMMPORT: 0
MATRIXCMSERVERNAME: <empty>
MATRIXNAME: <empty>
NPENABLED: 0
PID: *****
QUIET: false
QUIETSIMPLE: false
ROLE: <empty>
RSINSTALLMODE: DefaultNativeMode
RSSHPINSTALLMODE: DefaultSharePointMode
RSSVCACCOUNT: <empty>
RSSVCPASSWORD: <empty>
RSSVCSTARTUPTYPE: Automatic
SAPWD: <empty>
SECURITYMODE: <empty>
SQLBACKUPDIR: <empty>
SQLCOLLATION: SQL_Latin1_General_CP1_CI_AS
SQLSVCACCOUNT: NT Service\MSSQL$SQLEXPRESS
SQLSVCPASSWORD: <empty>
SQLSVCSTARTUPTYPE: Manual
SQLSYSADMINACCOUNTS: MH\gt038676t
SQLTEMPDBDIR: <empty>
SQLTEMPDBLOGDIR: <empty>
SQLUSERDBDIR: <empty>
SQLUSERDBLOGDIR: <empty>
SQMREPORTING: false
TCPENABLED: 0
UIMODE: AutoAdvance
UpdateEnabled: true
UpdateSource: MU
X86: false
Configuration file: C:\Program Files\Microsoft SQL Server\110\Setup Bootstrap\Log\20131223_144132\ConfigurationFile.ini
Detailed results:
Feature: Management Tools - Complete
Status: Passed
Feature: Client Tools Connectivity
Status: Passed
Feature: Client Tools SDK
Status: Passed
Feature: Client Tools Backwards Compatibility
Status: Passed
Feature: Management Tools - Basic
Status: Passed
Feature: Database Engine Services
Status: Failed: see logs for details
Reason for failure: An error occurred during the setup process of the feature.
Next Step: Use the following information to resolve the error, uninstall this feature, and then run the setup process again.
Component name: SQL Server Database Engine Services Instance Features
Component error code: 0x851A0019
Error description: Could not find the Database Engine startup handle.
Error help link: http://go.microsoft.com/fwlink?LinkId=20476&ProdName=Microsoft+SQL+Server&EvtSrc=setup.rll&EvtID=50000&ProdVer=11.0.3000.0&EvtType=0xE53883A0%400xBE03358B%401306%4025&EvtType=0xE53883A0%400xBE03358B%401306%4025
Feature: SQL Server Replication
Status: Failed: see logs for details
Reason for failure: An error occurred for a dependency of the feature causing the setup process for the feature to fail.
Next Step: Use the following information to resolve the error, uninstall this feature, and then run the setup process again.
Component name: SQL Server Database Engine Services Instance Features
Component error code: 0x851A0019
Error description: Could not find the Database Engine startup handle.
Error help link: http://go.microsoft.com/fwlink?LinkId=20476&ProdName=Microsoft+SQL+Server&EvtSrc=setup.rll&EvtID=50000&ProdVer=11.0.3000.0&EvtType=0xE53883A0%400xBE03358B%401306%4025&EvtType=0xE53883A0%400xBE03358B%401306%4025
Feature: SQL Browser
Status: Passed
Feature: SQL Writer
Status: Passed
Feature: SQL Client Connectivity
Status: Passed
Feature: SQL Client Connectivity SDK
Status: Passed
Rules with failures:
Global rules:
Scenario specific rules:
Rules report file: C:\Program Files\Microsoft SQL Server\110\Setup Bootstrap\Log\20131223_144132\SystemConfigurationCheck_Report.htm -
PL/SQL 101 : Cursors and SQL Projection
PL/SQL 101 : Cursors and SQL Projection
This is not a question, it's a forum article, in reponse to the number of questions we get regarding a "dynamic number of columns" or "rows to columns"
There are two integral parts to an SQL Select statement that relate to what data is selected. One is Projection and the other is Selection:-
Selection is the one that we always recognise and use as it forms the WHERE clause of the select statement, and hence selects which rows of data are queried.
The other, SQL Projection is the one that is less understood, and the one that this article will help to explain.
In short, SQL Projection is the collective name for the columns that are Selected and returned from a query.
So what? Big deal eh? Why do we need to know this?
The reason for knowing this is that many people are not aware of when SQL projection comes into play when you issue a select statement. So let's take a basic query...
First create some test data...
create table proj_test as
select 1 as id, 1 as rn, 'Fred' as nm from dual union all
select 1,2,'Bloggs' from dual union all
select 2,1,'Scott' from dual union all
select 2,2,'Smith' from dual union all
select 3,1,'Jim' from dual union all
select 3,2,'Jones' from dual
... and now query that data...
SQL> select * from proj_test;
ID RN NM
1 1 Fred
1 2 Bloggs
2 1 Scott
2 2 Smith
3 1 Jim
3 2 Jones
6 rows selected.
OK, so what is that query actually doing?
To know that we need to consider that all queries are cursors and all cursors are processed in a set manner, roughly speaking...
1. The cursor is opened
2. The query is parsed
3. The query is described to know the projection (what columns are going to be returned, names, datatypes etc.)
4. Bind variables are bound in
5. The query is executed to apply the selection and identify the data to be retrieved
6. A row of data is fetched
7. The data values from the columns within that row are extracted into the known projection
8. Step 6 and 7 are repeated until there is no more data or another condition ceases the fetching
9. The cursor is closed
The purpose of the projection being determined is so that the internal processing of the cursor can allocate memory etc. ready to fetch the data into. We won't get to see that memory allocation happening easily, but we can see the same query being executed in these steps if we do it programatically using the dbms_sql package...
CREATE OR REPLACE PROCEDURE process_cursor (p_query in varchar2) IS
v_sql varchar2(32767) := p_query;
v_cursor number; -- A cursor is a handle (numeric identifier) to the query
col_cnt integer;
v_n_val number; -- numeric type to fetch data into
v_v_val varchar2(20); -- varchar type to fetch data into
v_d_val date; -- date type to fetch data into
rec_tab dbms_sql.desc_tab; -- table structure to hold sql projection info
dummy number;
v_ret number; -- number of rows returned
v_finaltxt varchar2(100);
col_num number;
BEGIN
-- 1. Open the cursor
dbms_output.put_line('1 - Opening Cursor');
v_cursor := dbms_sql.open_cursor;
-- 2. Parse the cursor
dbms_output.put_line('2 - Parsing the query');
dbms_sql.parse(v_cursor, v_sql, dbms_sql.NATIVE);
-- 3. Describe the query
-- Note: The query has been described internally when it was parsed, but we can look at
-- that description...
-- Fetch the description into a structure we can read, returning the count of columns that has been projected
dbms_output.put_line('3 - Describing the query');
dbms_sql.describe_columns(v_cursor, col_cnt, rec_tab);
-- Use that description to define local datatypes into which we want to fetch our values
-- Note: This only defines the types, it doesn't fetch any data and whilst we can also
-- determine the size of the columns we'll just use some fixed sizes for this example
dbms_output.put_line(chr(10)||'3a - SQL Projection:-');
for j in 1..col_cnt
loop
v_finaltxt := 'Column Name: '||rpad(upper(rec_tab(j).col_name),30,' ');
case rec_tab(j).col_type
-- if the type of column is varchar2, bind that to our varchar2 variable
when 1 then
dbms_sql.define_column(v_cursor,j,v_v_val,20);
v_finaltxt := v_finaltxt||' Datatype: Varchar2';
-- if the type of the column is number, bind that to our number variable
when 2 then
dbms_sql.define_column(v_cursor,j,v_n_val);
v_finaltxt := v_finaltxt||' Datatype: Number';
-- if the type of the column is date, bind that to our date variable
when 12 then
dbms_sql.define_column(v_cursor,j,v_d_val);
v_finaltxt := v_finaltxt||' Datatype: Date';
-- ...Other types can be added as necessary...
else
-- All other types we'll assume are varchar2 compatible (implicitly converted)
dbms_sql.DEFINE_COLUMN(v_cursor,j,v_v_val,2000);
v_finaltxt := v_finaltxt||' Datatype: Varchar2 (implicit)';
end case;
dbms_output.put_line(v_finaltxt);
end loop;
-- 4. Bind variables
dbms_output.put_line(chr(10)||'4 - Binding in values');
null; -- we have no values to bind in for our test
-- 5. Execute the query to make it identify the data on the database (Selection)
-- Note: This doesn't fetch any data, it just identifies what data is required.
dbms_output.put_line('5 - Executing the query');
dummy := dbms_sql.execute(v_cursor);
-- 6.,7.,8. Fetch the rows of data...
dbms_output.put_line(chr(10)||'6,7 and 8 Fetching Data:-');
loop
-- 6. Fetch next row of data
v_ret := dbms_sql.fetch_rows(v_cursor);
-- If the fetch returned no row then exit the loop
exit when v_ret = 0;
-- 7. Extract the values from the row
v_finaltxt := null;
-- loop through each of the Projected columns
for j in 1..col_cnt
loop
case rec_tab(j).col_type
-- if it's a varchar2 column
when 1 then
-- read the value into our varchar2 variable
dbms_sql.column_value(v_cursor,j,v_v_val);
v_finaltxt := ltrim(v_finaltxt||','||rpad(v_v_val,20,' '),',');
-- if it's a number column
when 2 then
-- read the value into our number variable
dbms_sql.column_value(v_cursor,j,v_n_val);
v_finaltxt := ltrim(v_finaltxt||','||to_char(v_n_val,'fm999999'),',');
-- if it's a date column
when 12 then
-- read the value into our date variable
dbms_sql.column_value(v_cursor,j,v_d_val);
v_finaltxt := ltrim(v_finaltxt||','||to_char(v_d_val,'DD/MM/YYYY HH24:MI:SS'),',');
else
-- read the value into our varchar2 variable (assumes it can be implicitly converted)
dbms_sql.column_value(v_cursor,j,v_v_val);
v_finaltxt := ltrim(v_finaltxt||',"'||rpad(v_v_val,20,' ')||'"',',');
end case;
end loop;
dbms_output.put_line(v_finaltxt);
-- 8. Loop to fetch next row
end loop;
-- 9. Close the cursor
dbms_output.put_line(chr(10)||'9 - Closing the cursor');
dbms_sql.close_cursor(v_cursor);
END;
SQL> exec process_cursor('select * from proj_test');
1 - Opening Cursor
2 - Parsing the query
3 - Describing the query
3a - SQL Projection:-
Column Name: ID Datatype: Number
Column Name: RN Datatype: Number
Column Name: NM Datatype: Varchar2
4 - Binding in values
5 - Executing the query
6,7 and 8 Fetching Data:-
1 ,1 ,Fred
1 ,2 ,Bloggs
2 ,1 ,Scott
2 ,2 ,Smith
3 ,1 ,Jim
3 ,2 ,Jones
1 ,3 ,Freddy
1 ,4 ,Fud
9 - Closing the cursor
PL/SQL procedure successfully completed.
So, what's really the point in knowing when SQL Projection occurs in a query?
Well, we get many questions asking "How do I convert rows to columns?" (otherwise known as a pivot) or questions like "How can I get the data back from a dynamic query with different columns?"
Let's look at a regular pivot. We would normally do something like...
SQL> select id
2 ,max(decode(rn,1,nm)) as nm_1
3 ,max(decode(rn,2,nm)) as nm_2
4 from proj_test
5 group by id
6 /
ID NM_1 NM_2
1 Fred Bloggs
2 Scott Smith
3 Jim Jones
(or, in 11g, use the new PIVOT statement)
but many of these questioners don't understand it when they say their issue is that, they have an unknown number of rows and don't know how many columns it will have, and they are told that you can't do that in a single SQL statement. e.g.
SQL> insert into proj_test (id, rn, nm) values (1,3,'Freddy');
1 row created.
SQL> select id
2 ,max(decode(rn,1,nm)) as nm_1
3 ,max(decode(rn,2,nm)) as nm_2
4 from proj_test
5 group by id
6 /
ID NM_1 NM_2
1 Fred Bloggs
2 Scott Smith
3 Jim Jones
... it's not giving us this 3rd entry as a new column and we can only get that by writing the expected columns into the query, but then what if more columns are added after that etc.
If we look back at the steps of a cursor we see again that the description and projection of what columns are returned by a query happens before any data is fetched back.
Because of this, it's not possible to have the query return back a number of columns that are based on the data itself, as no data has been fetched at the point the projection is required.
So, what is the answer to getting an unknown number of columns in the output?
1) The most obvious answer is, don't use SQL to try and pivot your data. Pivoting of data is more of a reporting requirement and most reporting tools include the ability to pivot data either as part of the initial report generation or on-the-fly at the users request. The main point about using the reporting tools is that they query the data first and then the pivoting is simply a case of manipulating the display of those results, which can be dynamically determined by the reporting tool based on what data there is.
2) The other answer is to write dynamic SQL. Because you're not going to know the number of columns, this isn't just a simple case of building up a SQL query as a string and passing it to the EXECUTE IMMEDIATE command within PL/SQL, because you won't have a suitable structure to read the results back into as those structures must have a known number of variables for each of the columns at design time, before the data is know. As such, inside PL/SQL code, you would have to use the DBMS_SQL package, just like in the code above that showed the workings of a cursor, as the columns there are referenced by position rather than name, and you have to deal with each column seperately. What you do with each column is up to you... store them in an array/collection, process them as you get them, or whatever. They key thing though with doing this is that, just like the reporting tools, you would need to process the data first to determine what your SQL projection is, before you execute the query to fetch the data in the format you want e.g.
create or replace procedure dyn_pivot is
v_sql varchar2(32767);
-- cursor to find out the maximum number of projected columns required
-- by looking at the data
cursor cur_proj_test is
select distinct rn
from proj_test
order by rn;
begin
v_sql := 'select id';
for i in cur_proj_test
loop
-- dynamically add to the projection for the query
v_sql := v_sql||',max(decode(rn,'||i.rn||',nm)) as nm_'||i.rn;
end loop;
v_sql := v_sql||' from proj_test group by id order by id';
dbms_output.put_line('Dynamic SQL Statement:-'||chr(10)||v_sql||chr(10)||chr(10));
-- call our DBMS_SQL procedure to process the query with it's dynamic projection
process_cursor(v_sql);
end;
SQL> exec dyn_pivot;
Dynamic SQL Statement:-
select id,max(decode(rn,1,nm)) as nm_1,max(decode(rn,2,nm)) as nm_2,max(decode(rn,3,nm)) as nm_3 from proj_test group by id order by id
1 - Opening Cursor
2 - Parsing the query
3 - Describing the query
3a - SQL Projection:-
Column Name: ID Datatype: Number
Column Name: NM_1 Datatype: Varchar2
Column Name: NM_2 Datatype: Varchar2
Column Name: NM_3 Datatype: Varchar2
4 - Binding in values
5 - Executing the query
6,7 and 8 Fetching Data:-
1 ,Fred ,Bloggs ,Freddy
2 ,Scott ,Smith ,
3 ,Jim ,Jones ,
9 - Closing the cursor
PL/SQL procedure successfully completed.
... and if more data is added ...
SQL> insert into proj_test (id, rn, nm) values (1,4,'Fud');
1 row created.
SQL> exec dyn_pivot;
Dynamic SQL Statement:-
select id,max(decode(rn,1,nm)) as nm_1,max(decode(rn,2,nm)) as nm_2,max(decode(rn,3,nm)) as nm_3,max(decode(rn,4,nm)) as nm_4 from proj_test group by id order by id
1 - Opening Cursor
2 - Parsing the query
3 - Describing the query
3a - SQL Projection:-
Column Name: ID Datatype: Number
Column Name: NM_1 Datatype: Varchar2
Column Name: NM_2 Datatype: Varchar2
Column Name: NM_3 Datatype: Varchar2
Column Name: NM_4 Datatype: Varchar2
4 - Binding in values
5 - Executing the query
6,7 and 8 Fetching Data:-
1 ,Fred ,Bloggs ,Freddy ,Fud
2 ,Scott ,Smith , ,
3 ,Jim ,Jones , ,
9 - Closing the cursor
PL/SQL procedure successfully completed.
Of course there are other methods, using dynamically generated scripts etc. (see Re: 4. How do I convert rows to columns?), but the above simply demonstrates that:-
a) having a dynamic projection requires two passes of the data; one to dynamically generate the query and another to actually query the data,
b) it is not a good idea in most cases as it requires code to handle the results dynamically rather than being able to simply query directly into a known structure or variables, and
c) a simple SQL statement cannot have a dynamic projection.
Most importantly, dynamic queries prevent validation of your queries at the time your code is compiled, so the compiler can't check that the column names are correct or the tables names, or that the actual syntax of the generated query is correct. This only happens at run-time, and depending upon the complexity of your dynamic query, some problems may only be experienced under certain conditions. In effect you are writing queries that are harder to validate and could potentially have bugs in them that would are not apparent until they get to a run time environment. Dynamic queries can also introduce the possibility of SQL injection (a potential security risk), especially if a user is supplying a string value into the query from an interface.
To summarise:-
The projection of an SQL statement must be known by the SQL engine before any data is fetched, so don't expect SQL to magically create columns on-the-fly based on the data it's retrieving back; and, if you find yourself thinking of using dynamic SQL to get around it, just take a step back and see if what you are trying to achieve may be better done elsewhere, such as in a reporting tool or the user interface.
Other articles in the PL/SQL 101 series:-
PL/SQL 101 : Understanding Ref Cursors
PL/SQL 101 : Exception Handlingexcellent article. However there is one thing which is slightly erroneous. You don't need a type to be declared in the database to fetch the data, but you do need to declare a type;
here is one of my unit test scripts that does just that.
DECLARE
PN_CARDAPPL_ID NUMBER;
v_Return Cci_Standard.ref_cursor;
type getcardapplattrval_recordtype
Is record
(cardappl_id ci_cardapplattrvalue.cardappl_ID%TYPE,
tag ci_cardapplattrvalue.tag%TYPE,
value ci_cardapplattrvalue.value%TYPE
getcardapplattrvalue_record getcardapplattrval_recordtype;
BEGIN
PN_CARDAPPL_ID := 1; --value must be supplied
v_Return := CCI_GETCUSTCARD.GETCARDAPPLATTRVALUE(
PN_CARDAPPL_ID => PN_CARDAPPL_ID
loop
fetch v_return
into getcardapplattrvalue_record;
dbms_output.put_line('Cardappl_id=>'||getcardapplattrvalue_record.cardappl_id);
dbms_output.put_line('Tag =>'||getcardapplattrvalue_record.tag);
dbms_output.put_line('Value =>'||getcardapplattrvalue_record.value);
exit when v_Return%NOTFOUND;
end loop;
END; -
Sqlcode and sql%rowcount as test conditions
I am translating a procedure from Ingres to oracle. I use the Oracle sqlcode and sql%rowcount
variables in place of Ingres's iierrornumber and iirowcount.
I replaced the ingres names with the oracle names, and now am wondering how how oracle uses these
values.
I am using DBMS_OUTPUT.PUT_LINE to display the contents of sqlcode and sql%rowcount.
What happens when I get "no rows selected". I run the same query in standalone command line
and get no rows selected .. and it's correct because I don't have data that matches).
But when I run it in the procedure, I don't get any values in sqlcode or sql%rowcount ... the
program just exits. The author was using the ingres code to test if there was data there and then
continue to do something else.
If you get no rows returned, does that mean, the sqlcode is 0 ? and the sql%rowcount is emtpy?
which is why my DBMS_OUTPUT.PUT_LINE doesn't display anything ?
my example........
before this section in the procedure ... after running a different select that returns data ...
I am using the DBMS to debug the values I have going in ...
DBMS_OUTPUT.Put_line ('BEFORE sqlcode is' || sqlcode);
DBMS_OUTPUT.Put_line ('BEFORE sqlrowcount is' || sql%rowcount);
Select value from table
where condition ='Y'; > this select will product "no rows return" and ignore the 2 DBMS_OUTPUTS beow ---
DBMS_OUTPUT.Put_line ('AFTER sqlcode is' || sqlcode);
DBMS_OUTPUT.Put_line ('AFTER sqlrowcount is' || sql%rowcount);
When i run the procedure... I get the DBMS BEFORE statements, and nothing afterwards ...
BEFORE sqlcode is 0
BEFORE sqlrowcount is 1 >>>> that is all that displays
Since I am getting the "no rows returned" ... how does oracle handle it to anyone's experience ?
Thank you. I am learning much from your comments and information.Thanks to your answers .... The procedure is below. I've had to hand type it in, so typos are my mistakes.
The procedure compiles . When there is data to be found, I get the DBSM_OUTPUT lines of code ....
msg_read is Y
sql_error is 0
row_count is 1
p_vol_id is 880091
When I enter in a file name that does not return ANY rows back I will get the msg_read
DBMS_OUTPUT line
msg_read is Y
Call completed.
It doesn't show any 0 for sqlcode or sql%rowcount
The original author used the Ingres return codes as input to process the rest of the code...
It seems like oracle bounces the procedure once there are no rows to be found.
I just added this part ....
having an exception in the clause shows that Oracle is bouncing it to the WHEN OTHERS
exception ...
Any ideas of how to get Oracle not to do this ?
I am trying to keep things simple, and all I am testing for is if I get records back the code does things,
if not, I do something else
create or replace procedure userfile(vms_fil_name IN varchar2, msg_read IN Varchar2)
authid current_user
is
p_vms_file_name varchar2(255);
p_vol_id varchar2(255);
p_orig_id varchar2(255);
p_incoming_message varchar2(255);
sql_error number;
n_count number;
begin
p_vms_fil_nam :=vms_fil_nam;
DBMS_OUTPUT.PUT_LINE ('msg read is '|| msg_read); >> verify incoming parameter
IF (msg_read ='Y')
then
select vol_id,
orig_id,
incoming_message
into p_vol_id,
p_orig_id,
p_incoming_message
from
table one_table a
where
a.vms_fil_nam = p.vms_fil_nam
and incoming_msg ='I';
n_count :='sql%rowcount';
sql_error :='sqlcode;
DBMS_OUTPUT.PUT_LINE ('row count is '|| n_count);
DBMS_OUTPUT.PUT_LINE ('sql_error is '|| sql_error);
DBMS_OUTPUT.PUT_LINE ('p_vol_id is '|| p_vol_id);
end if;
exception >>>> just added this part as the last test....
when others
DBMS_OUTPUT.PUT_LINE ('other condition has been met');
end;
call userfile ('GEORGE','Y'); -
Error while running PL/SQL XML Parser Samples
every time try to run the PL/SQL XML Parser Sample program i get this error no file or directory, though the file and dir exists, i have the permissions setup as in readme.txt do i need to do any chnages to init.ora,
pls advice
Thanks,
SQL> exec test
begin test; end;
ERROR at line 1:
ORA-20100: Error occurred while parsing: No such file or directory
ORA-06512: at "SCOTT.XMLPARSER", line 22
ORA-06512: at "SCOTT.XMLPARSER", line 69
ORA-06512: at "SCOTT.TEST", line 13
ORA-06512: at line 1Which examples in particular? Thanks.
-
Unable to connect to report server - on Win 8.1 and SQL Server 2014
unable to connect to report server on Win 8.1 and SQL Server 2014 version
Microsoft SQL Server 2014 - 12.0.2000.8 (Intel X86)
Feb 20 2014 19:20:46
Copyright (c) Microsoft Corporation
Express Edition on Windows NT 6.3 <X64> (Build 9600: ) (WOW64)
ThanksHi,
I suggest you check out the following article for the steps of installing
Database Engine Services and SQL Server Reporting Service.
http://msdn.microsoft.com/en-us/library/ms143711.aspx
Best Regards,
Tracy
Tracy Cai
TechNet Community Support -
XL Reporter in Sap 8.8 PL 16running On Server 2008 R2 and SQL 2008 R2
Within the above environment (XL Reporter in Sap 8.8 PL 16 running On Server 2008 R2 and SQL 2008 R2)
Xl reporter is loading and appears to work fine until you look at the spreadsheet result
It is completely blank except for the titles
Yet Excel Macros and the attachment paths are defined correctly
As can be seen when we export any report to Excel successfully
I've had another consultant look at my setup carefully just to be sure
He can't figure out why
Anyone with similar results
I would appreciate any comments
Georges OstiguyEven something as simple as a list of BP 's and phone number gives a blank report
When my friend who knows xl reporter much more than I do tried, he managed to get the
BP numbers but nothing further
I then brought him to another server with the same configuration , same result
He then tried it on one of his own servers with similar config = same result
Server 2008 Standard R2 64bit
MSSQL 2008 R2
SAP 8.8 PL 16
Sample demo database
I don't get it -
HOW TO : Writing simple scripts in SQL Developer??
Here is a simple script I run in SQL*Plus...
the first SQL prints the QCSID, the second script prompts for a SID which is the QCSID from the first SQL... once provided it gives me the output I want...
I just cannot do this in SQL DEVELOPER..... or Can I?
select distinct qcsid from v$px_session;
select sid,serial#,qcsid,qcserial#,degree,req_degree from v$px_session
where qcsid in (select qcsid from v$px_session where sid='&sid');Ganesh,
Simple substitution and defines work in SQL Developer scripts.
I think the problem with running your script is that the first query results are not displayed before the window asking for the '&sid' value. That is the output to the 'screen output' tab is buffered and not synchronized with additional pop up (or dialog) windows. Is that correct?
Currently:
You can use substitution variables in the f5 'Run Script' but no bind variables.
You can use bind variables in the f9 'Execute Statement' but no substitution statements.
You can use anonymous PLSQL blocks, and packaged functions and procedures, so you can store per session state in PLSQL packages.
-Turloch -
Uninstall SQL Server 2008 Express R2 and SQL Server 2012 unsuccessfully
HI
I want to uninstall SQL Server 2008 R2 and SQL Server 2012 but it was successful.
When I try to uninstall 2008 R2
It shows the sqlsupport.msi can't find
Warning 26003. Microsoft Server 2008 R2 Setup Support Files cannot be uninstalled because the following products are installed:
SQL Server 2008 R2 SP2 Common Files are installed
SQL Server 2008 R2 Dabtabase Engine Shared
Microsfot SQL Server R2 RsFx Driver
SQL Server 2008 R2 SP2 Database Engine Services
When I uninstall 2012, it shows the SQL Browser has been installed.
Warning 26003. Microsoft SQL sERVER 2012 Setup Support Files cannot be uninstalled because the following products are installed:
SQL Server Browser for SQL Server 2012I tried to uninsall SQL Server 2008 R2 and SQL Server 2012.
Yes, I did what you said : right click on Microsoft SQL Server 2008 R2 and then uninstall.
System pop up window says :
Warning 26003. Microsoft SQL Server 2008 R2 Setup Support Files cannot be uninstalled because the following products are installed:
SQL Server 2008 R2 SP2 Common Files are installed
SQL Server 2008 R2 Dabtabase Engine Shared
Microsfot SQL Server R2 RsFx Driver
SQL Server 2008 R2 SP2 Database Engine Services
I hope you are uninstalling from Add remove program. Can you first uninstall
SQL Server 2008 R2 SP2 Common Files are installed
SQL Server 2008 R2 Dabtabase Engine Shared
Microsfot SQL Server R2 RsFx Driver
SQL Server 2008 R2 SP2 Database Engine Services
And then uninstall the
Microsoft SQL Server 2008 R2
Please mark this reply as answer if it solved your issue or vote as helpful if it helped so that other forum members can benefit from it
My Technet Wiki Article
MVP -
Differernce between SQL Tuning Pack and SQL Tuning Advisor, Access Advisor
Hi. all.
I have been using "TKPROF" in order to tune SQL till now.
I am learning 10g, and would like to know the difference
between "1. SQL Tuning Pack" and "2. SQL Tuning Advisor, Access Advisor".
Are they same thing? If not, what are the differences in their "functionality"?
All I know is that SQL Tuning Pack is not free, and SQL Tuning Advisor
is packed with default installation. Right???
Could you give me some html links or your "any advice and experience"?
Thanks in advance. Have a good day.
Best Regards.Hi,
There are alternatives to the Oracle tuning packs, and remember the SQL advisors are relatuively simple, finding missing indexes and recommending MV's.
A human can do a better job if you use the right techniques.
BTW, you can get a SQLTuning advisor license online for only $3k:
http://www.dba-oracle.com/oracle_news/2005_3_17_dql_tuning_access_advisor_licenses_available_online.htm
SHAMELESS PITCH! - I wrote a book that describes techniques for tuning with scripts, instead of the advisors, if you are interested:
http://www.rampant-books.com/book_2005_1_awr_proactive_tuning.htm
Hope this helps. . . .
Donald K. Burleson
Oracle Press author
Maybe you are looking for
-
Problem using KeyListener in a JFrame
Let's see if anyone can help me: I'm programming a Maze game, so I use a JFrame called Window, who extends JFrame and implements ActionListener, KeyListener. Thw thing is that the clase Maze is a JPanel, so I add it to my Frame and add the KeyListene
-
I just updated to OS X 10.9.3 and lost 3 years of work
I was in a hurry to instal new software that needed 10.9.3 so I quickly updated it not realising that I needed to make a back up. I now am left with a completely clean slate on my computer. I seem to have all of my Applications but with not files. A
-
My computer won't recognise my iPod Classic, even in disk mode?
I have a 160GB 6th generation iPod classic and my itunes won't recognise it. When I plug it into my laptop the laptop makes the noise as to say it has a new device connected, but it won't show up in my windows explorer or in my itunes. I have googled
-
Will I lose Bootcamp on a clean install?
I am getting ready to do a clean install of Leopard. I have bootcamp installed on my MacBook Pro. Will the clean install affect the partition at all? Will i have to install the windows os again after the clean install of 10.5? phil
-
I have lost my camera on my iphone 4s i tried resetting my desktop and still no camera
i have lost my camera on my iphone 4s i tried resetting my desktop and still no camera