SQL insert into with Hsqldb Script - sql

I am trying to initialise my hsqldb with some default data but seem to be having a problem with identity and timestamp columns.
I just realised that I probably wasn't clear what I meant when I said "script". I am meaning the command line argument that you pass to hsqldb to generate your database at startup. I can successfully run the query inside DbVisualiser or some other database management tool.
I have a table with the following definition:
create table TableBob (
ID int NOT NULL identity ,
FieldA varchar(10) NULL,
FieldB varchar(50) NOT NULL,
INITIAL_DT timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL);
I can successfully create this table using the script but trying to insert a record doesn't work. Below is what I would consider valid sql for the insert since the ID and INITIAL_DT fields are Identity and Default columns). Strangely it inserts null into every column even though they are defined as NOT NULL....
e.g.
INSERT INTO TableBob (FieldA, FieldB) VALUES ('testFieldA', 'testFieldB');
Thanks for your help

Please try with HSQLDB's DatabaseManagerSwing (you can double click on the hsqldb.jar to start the database manager). First execute the CREATE TABLE statement, then the INSERT statement, finally the SELECT statement.
It should show the correct results.
If you want to use a script to insert data, use the SqlTool.jar which is available in the HSQLDB distribution zip package. See the guide: http://hsqldb.org/doc/2.0/util-guide/

Related

Need to create a Run id for each package run in SSIS Package

I have an SSIS Package that runs a query and inserts values into a different table. Each time the package runs, I want to create a unique RunID for the results of that run. Here are the columns from my table. I have tried this using the Execute SQL Task and setting up the User::RunID variable but, I believe I am doing something wrong. Can anyone provide step by step instructions on how to do this?
You need 2 tables for this.
create table runs(
runID int identity primary key,
runDateTime datetime default getdate()
)
create table runReturns(
runReturnsID int identity primary key,
runID int not null,
[the rest of the data set]
)
In ssis, start with an execute SQL.
Add this query...
insert into runs (runDateTime) values(?);
select SCOPE_IDENTITY()
Map the parameter (?) to Now();
Change the result set to single row and map the first column to a parameter called runID.
Now create a data flow.
Insert your query into a sql source.
Add a derived column and map a new column to runID.
Finally, add a destination to your table and map accordingly.
Adding a completely sql answer to compliment as an alternative since there are no transformations at all:
Same 2 tables:
create table runs(
runID int identity primary key,
runDateTime datetime default getdate()
)
create table runReturns(
runReturnsID int identity primary key,
runID int not null,
[the rest of the data set]
)
Create a Job.
Add a step and base it on SQL.
declare #runID int;
insert into runs(runDateTime) values(getdate());
select #runID = scope_idenity();
insert into runReturns(
runID, [rest of your columns])
select #runID
, [rest of your columns]
from [rest of your query]
An approach that might solve the issue, is the system scoped variable ServerExecutionID By default, System scoped variables are hidden in the Variables menu but you can expose them by clicking the Grid options button (rightmost of the 5).
If you reference that variable using the appropriate placeholder (? for OLE/ODBC or a named parameter for ADO) and map to the variable, then every server execution will have a monotonically increasing number associated to it. Runs from Visual Studio or outside of the SSISDB, will always have a value of 0 associated to them but given that this is only encountered during development, this might address the issue.
Sample query based on the newer picture
INSERT INTO dbo.RunMapTable
SELECT ? AS RunID
, D.Name
FROM
(
VALUES ('A')
, ('B')
, ('C')
, ('D')
)D([name];
Parameter Mapping
0 -> System::ServerExecutionID
As an added bonus, you can then tie your custom logging back to the native logging in the SSISDB.

Azure Data Factory v2 Not Null Columns in sink

I'm trying out Azure Data Factory v2 and I want to pipe data from an SQL source to an Oracle sink.
My problem is, that I have several Not Null columns in my Oracle tables which specify for example the date and time of which a dataset is loaded into Oracle. These columns however aren't existant in the SQL tables so when I want to start the pipeline I get the error that these columns can't be null in the Oracle sink.
My question is now, is it possible to artificially add these columns during the pipeline run so that these columns get filled by the Data Factory?
Can I use a stored procedure or a custom activity for that?
Or do I have to create a Powershell script which "hardcodes" the values I want to add to the source?
You can accomplish this in ADFv2 using a query against your source dataset in the Copy activity to insert values.
Using the table ex_employee, with the following configuration in each database:
Source table (SQL):
ID int not null,
Name nvarchar(25) not null
Sink table (Oracle):
ID number(p,0) not null,
Name nvarchar2(25) not null,
CreatedDate timestamp not null
In the Source configuration on your Copy activity in ADF, you would select the Query option under Use Query, and input a query, such as:
SELECT ID, Name, CURRENT_TIMESTAMP AS CreatedDate FROM ex_employee
This will take the existing values from your SQL table, and insert a default value into the result set, which can then be inserted into your Oracle sink.
Does this column has default value ? can you add default to this column then try? I not familiar with oracle pipe data however a similar approach in the below example adding a default value to a not null column.
drop table ex_employee
/
create table ex_employee (id number(1) null ,name varchar2(100) default 'A' not null )
/
insert into ex_employee(id)
select 1 from dual
/
commit
/
selecT * from ex_employee where id=1

PostgreSQL: INSERT INTO syntax error

I'm following an older tutorial learning Postgres, so it's possible maybe something has changed since it was published. In the tutorial (using psql) I create a table then do some insert statements. Here is the tutorial and corresponding psql commands that cause error:
http://www.postgresqlforbeginners.com/2010/11/create-table-and-constraints.html
create table people(
id int PRIMARY KEY,
name varchar NOT NULL
);
insert into people(0,'Steve Jobs');
insert into people(1,'Mike Markkula');
insert into people(2,'Mike Scott');
insert into people(3,'John Sculley');
insert into people(4,'Michael Spindler');
insert into people(5,'Gil Amelio');
insert into people(6,'Mike Scott');
I get this error for each insert statement:
ERROR: syntax error at or near "0"
LINE 1: insert into people(0,'Steve Jobs');
^
I've tried copy pasting, capitalizing the sql commands (ie INSERT), running the command from shell outside of psql, adding spaces, using " instead of ' quotes... All result in the same errors. Has something changed or am I possibly doing something wrong?
The problem is the missing values (as noted in a comment).
I want to make some suggestions. First, whenever you use insert, you should always list the columns. This is especially important if you are learning the language -- you should be learning good habits.
Second, you don't need multiple inserts. A shorter way to insert multiple rows is:
insert into people (id, name)
values (0,'Steve Jobs'),
(1,'Mike Markkula'),
(2,'Mike Scott'),
(3,'John Sculley'),
(4,'Michael Spindler'),
(5,'Gil Amelio'),
(6,'Mike Scott');
And you should learn about serial. A more common way to write this code would be:
create table people (
id serial PRIMARY KEY,
name varchar NOT NULL
);
insert into people (name)
values ('Steve Jobs'),
('Mike Markkula'),
('Mike Scott'),
('John Sculley'),
('Michael Spindler'),
('Gil Amelio'),
('Mike Scott');
The id is assigned automatically by the database (starting at 1 rather than 0).
I should add: I am personally uncomfortable with having varchar without a length. This is perfectly fine in Postgres, but some databases would interpret it as varchar(1).

SQL Server - Generate script without primary key

I'm trying to make a generated script of my data (I mean, all the INSERT INTO commands).
Because access permissions, I can't do a SET IDENTITY_INSERT TABLE OFF and ON (I'm using the user application in Staging)
So, there is a way to make this script in SQL Server Manager and avoid the field with the primary key?
I set to false all properties (primary, unique, etc), but the script is still sending this field (For e.g., RecID 1, 2, 3, etc).
I'm using SQL Server 2012.
My configuration for the script:
Results I get:
SET IDENTITY_INSERT -TABLE- ON
INSERT INTO TABLE (ID,Field1) VALUES (1,'value')
Any solution (except for removing it with Notepad++) is appreciated.
A bit of a work around, but sometimes useful quick and dirty way of doing these things:
SELECT 'INSERT INTO TABLE (Field1) VALUES (''' + Field1 + ''')' FROM TABLE
The result set will be a row for each insert statement for every row in the table TABLE. The INSERT statement is generated from concatenating the INSERT statement text with the values in Field1.
There is another way to do this which is a bit more automatic and a lot less faffy when you have a lot of columns of different data types; given a table T:
(
ID int identity,
C1 nvarchar(255),
C2 datetime
...
)
...select everything except the identity column into a new table:
select C1, C2, ... into InterimTable from T
Then:
Run the Generate Scripts wizard on InterimTable.
Use whatever tool you have to search the SQL for InterimTable and replace with T
Run

Getting the identity value from a table with single identity column in FitNesse

Does FitNesse/dbFit support inserting into a table with a single identity column and then returning that value into a variable?
For example:
create table TestTable ( ID int identity(1,1) )
!|Insert|TestTable|
|ID? |
|>>TestID |
results in the following exception:
System.Data.SqlClient.SqlException (0x80131904): Incorrect syntax near ')'.
Is there another way to accomplish this?
Your problem is not with Fitnesse, but with the SQL engine. If you try the same query in SQL Server Management Studio you will receive the same error. Thus, given the restriction that you cannot use set identity_insert on the question you really need to ask is how to insert a record with no insertable fields in SQL Server independent of Fitnesse? This StackOverflow post provides a simple answer:
INSERT INTO #TempTable DEFAULT VALUES
Having that in hand, now the task is to map this to Fitnesse. Here is one solution:
!|execute|CREATE TABLE #TestTable ( ID int identity(1,1) )|
!|execute|INSERT INTO #TestTable DEFAULT VALUES|
!|query|SELECT TOP 1 ##IDENTITY [ID] FROM #TestTable|
|ID? |
|>>TestID |
Fitnesse's INSERT command does not support the default construct, so you must switch to the more general EXECUTE command to do the insert. That, however, does not return results so you cannot glean the auto-inserted ID value directly. The final query gives you one way to grab the just-inserted identity value.