how to create database using java code? - database

I want to create a database using java code, I have the database script file, I will read in a string and I will execute it but I want to know if I can use executequery or executeupdate???
and if this method is right or there is another one???

The method signature for the executeUpdate method of the Statement interface is:
int executeUpdate(String sql)
throws SQLException
executeUpdate executes the given SQL statement, which may be an INSERT, UPDATE, or DELETE statement or an SQL statement that returns nothing, such as an SQL DDL statement.
So to define a table or define an index, you would pass the SQL string to the executeUpdate method.
You can only execute one SQL statement at a time using the executeUpdate method, so if you're reading a file with more than one SQL statement, you'll have to parse the file and execute each SQL statement, one at a time.

Related

SQL Server : Create Procedure in DB without using [GO]

I want to execute this (simplified) query using node-mssql that executes in SQL Server 2017 fine:
USE [Journal]
[GO]
CREATE PROCEDURE [dbo].[EventDelete]
#NotificationID INT
AS
DELETE Notification
WHERE NotificationID = #NotificationID
[GO]
node-mssql declares syntax error using [GO] and requires semicolon, therefore I try this:
USE [Journal];
CREATE PROCEDURE [dbo].[EventDelete]
#NotificationID INT
AS
DELETE Notification
WHERE NotificationID = #NotificationID;
Now we get error:
CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
So let's try this:
CREATE PROCEDURE [Journal].[dbo].[EventDelete]
#NotificationID INT
AS
DELETE Notification
WHERE NotificationID = #NotificationID;
Now we get
RequestError: 'CREATE/ALTER PROCEDURE' does not allow specifying the database name as a prefix to the object name.
Naturally without any DB declaration it attempts to attach to the master error:
CREATE PROCEDURE permission denied in database 'master'.
So writing the question really works at setting one's thoughts straight.
The reason is the stored procedure requires to be created in one batch, which [GO] signifies, with nothing else.
Execute USE [Journal] as one batch using the .batch('USE [Journal]') method and then the SQL to CREATE PROCEDUCE as a new .batch(...) execution, sequentially.
Unless there is another method within node-mssql which allows for multi-batch executions?

SSIS - use full resultset of execute sql task as input to another execute sql task

I have an SSIS package with two Execute SQL Tasks, the first one has a simple select statement to select a single column from a table, the result set is an ID column which I have to use as input to the second Execute SQL Task to get records from another table which matching IDs. Basically I have to implement following SQL statement in SSIS:
SELECT * FROM TableB WHERE ID IN
(
SELECT ID FROM TableA
)
How can I configure the second Execute SQL task to use the result set of first Execute SQL task as input to execute an SQL statement like mentioned above?
One solution would be to populate an object variable with the result of the first Execute SQL Task.
Then have a Script Task which loops through the object variable and dynamically builds the SQL String for the second Execute SQL Task, and puts that into a string variable.
Then finally, the second Execute SQL Task uses the string variable for its SQL query.

Incorrect syntax near '#StartDate'. Statement(s) could not be prepared

I try to get pdf report by stimulsoft but get this error.
Incorrect syntax near '#StartDate'. Statement(s) could not be prepared
I test my stored procedure and table function in sqlserver and worked currently.but when I try to run VeiwData in dictionary window get me this error.
Query text in stimulsoft :
execute ProceGetCharterReportPdf (#StartDate,#endDate,#top,#AgencyName)
Type of #StartDate,#endDate,#AgencyName is nvarchar in report file and stored procedure and function .Type of #top is int.
It's going to sound daft, but try adding your schema name if you're calling a function;
execute dbo.ProceGetCharterReportPdf(#StartDate,#endDate,#top,#AgencyName)
You have to use the schema name when calling a function I believe, some further reading below;
Is it possible to call a user-defined function without the schema name?
Is there a way to use a function on a Microsoft SQL Server Query without using "dbo." before the function?

VB.Net Upgrade Stored Procedure Call to Oracle Database moRs.Open(moCmd)

I have a program that was written in VB6 and we are trying to convert to VB.Net. When calling stored procedures in Microsoft SQL 2012 and in Oracle using this .Open() method, Microsoft SQL executes fine while Oracle does not. Here is a snippet of code:
Private moRs As ADORecordSetHelper = New ADORecordSetHelper("")
Private moCmd As DbCommand 'where moCmd has the procedure call and parameters
moRs.Open(moCmd)
Here is the definition of .Open()
Public Sub Open(command As System.Data.Common.DbCommand)
Member of UpgradeHelpers.DB.ADO.ADORecordSetHelper
This all works for Microsoft SQL just not with Oracle. I found execute moCmd.ExecuteNonQuery() will work execute the stored procedure, but I need to get the recordset in certain instances as a result.
Is there a way to use moRs.Open(moCmd) for both Microsoft and Oracle? If not, how could I accomplish the same task of getting back the recordsets?
UPDATE: I have figured out that the .Open() method uses Execute Reader which is working fine for the Microsoft SQL that strictly returns a recordset. The Oracle stored procedure is inserting rows and returning values from the procedure. Doing an Execute NonQuery on the same object (moCmd) executes just fine, but all I am able to get from this method is the number of rows affected and not the other information that I need. Thoughts on why the Execture Reader doesn't work? My search says that it should work for all types of SQL; insert, delete, update, etc.

How to Pass Parameters to ORACLE stored procedure in SSIS packages

I'm using execute sql task to call a Oracle stored procedure,I want to pass a parameter to a oracle stored procedure
Query Inside Execute sql task:-
BEGIN
PKG_METRICS.GET_STUDY_METRIC(#myparameter, 'FIRST_SITE_SELECTED', 'LAST_SITE_SELECTED', 6, null,'SITE');
END;
Please let me know your comments...
you should go to Expressions in SQL Task,
then add expression to SqlStatementSource
and put this code inside dialog window:
"BEGIN
PKG_METRICS.GET_STUDY_METRIC("+#var_myparameter+", 'FIRST_SITE_SELECTED', 'LAST_SITE_SELECTED', 6, null,'SITE');
END;"
Before that you should add string variable #var_myparameter.
You can build the sql expression as a string as suggested above, but you can also pass parameters to Oracle if they are inputs to the ole db command in the pipeline. Create an OLE DB Command block. In the component properties enter the sql and place ? marks where parameters should be. Next go to input and output properties. Expand OLE DB command Input and add an external column for each parameter you need to pass. Order the MappedColumnId tabs from top to bottom starting with zero. Map them to your inputs on the column mappings tab.
If your parameter is just a parameter in SSIS and not in the pipeline you could either add it to the pipeline with a simple derived column. You can also just execute the proc from a script component using your favorite oracle access dll.
No perfect options, but there are ways to do this!
In addition to chrisPfeif518 :
for Oracle (in my case) , in OLE DB Command task , i used syntax:
{call ... }
for your case should be :
{call PKG_METRICS.GET_STUDY_METRIC(?, ?, ?, ?, ?,?)}

Resources