I am stuck in creating bcp format file. My client is using MSSQL 2005 and I am remotely connecting through RDC. I am creating format file on the target client MSSQL Server using the following command,
bcp myDatabase.TableName format nul -c -x -f someFile..xml -T
but the it prompt me with error
An Error has occured while establishing a connection to the server.
My View pointing to a table having structure as under:
Colum Name DataType
SKU Varchar(20)
ASIN Varchar(20)
Price Float
Quantity Int
MarketplaceTitle Varchar(50)
Note: I have been through many similar questions but had no luck.
Anyone can please provide me with a format file for the above View?
Thanks in advance.
It sounds like your issue is actually connecting to your database, not the format file.
I've created a quick test:
-- DROP TABLE dbo.Test
CREATE TABLE dbo.Test(
SKU Varchar(20),
[ASIN] Varchar(20),
Price Float,
Quantity Int,
MarketplaceTitle Varchar(50)
);
and here's a slightly more verbose syntax:
bcp YourDatabaseName.dbo.Test format nul -c -x -f c:\YourDir\format.xml -T -S YourServerName
so for me, this looks like this:
bcp sandbox.dbo.Test format nul -c -x -f c:\Test\format.xml -T -S "(local)"
Here's the file generated:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="20" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="2" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="20" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
<FIELD ID="3" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="30"/>
<FIELD ID="4" xsi:type="CharTerm" TERMINATOR="\t" MAX_LENGTH="12"/>
<FIELD ID="5" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="50" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="SKU" xsi:type="SQLVARYCHAR"/>
<COLUMN SOURCE="2" NAME="ASIN" xsi:type="SQLVARYCHAR"/>
<COLUMN SOURCE="3" NAME="Price" xsi:type="SQLFLT8"/>
<COLUMN SOURCE="4" NAME="Quantity" xsi:type="SQLINT"/>
<COLUMN SOURCE="5" NAME="MarketplaceTitle" xsi:type="SQLVARYCHAR"/>
</ROW>
</BCPFORMAT>
HTH.
Related
I wan't my build script to be set with an environment variable that sets the schema name for the following changeSet:
<createTable tableName="actor" schemaName="mySchema">
<column autoIncrement="true" name="id" type="INTEGER">
<constraints nullable="false" primaryKey="true" primaryKeyName="actor_pkey"/>
</column>
<column name="firstname" type="VARCHAR(255)"/>
<column name="lastname" type="VARCHAR(255)"/>
<column name="twitter" type="VARCHAR(15)"/>
</createTable>
I launch Liquibase from the command line in my build script.
To pass arguments that will change from environment to environment, Liquibase uses property substitution. This requires 2 changes to your existing process.
The first is to replace literal values with a variable:
I want my build script to be set with an environment variable that sets the schema name for the following changeSet (we will use the property name of SCHEMA below):
<createTable tableName="actor" schemaName="${SCHEMA}">
<column autoIncrement="true" name="id" type="INTEGER">
<constraints nullable="false" primaryKey="true" primaryKeyName="actor_pkey"/>
</column>
<column name="firstname" type="VARCHAR(255)"/>
<column name="lastname" type="VARCHAR(255)"/>
<column name="twitter" type="VARCHAR(15)"/>
</createTable>
Then we can pass a value for SCHEMA during command line creation, and to prove what value will be substituted, I used (note lookup is the name of the schema I want to substitute):
liquibase --changeLogFile=“sample.xml” updateSQL -DSCHEMA=lookup
And here is a snippet of the output which will show that the schema has been replaced with the value of lookup:
....
CREATE TABLE lookup.actor (id INTEGER GENERATED BY DEFAULT AS IDENTITY NOT NULL, firstname VARCHAR(255), lastname VARCHAR(255), twitter VARCHAR(15), CONSTRAINT actor_pkey PRIMARY KEY (id));
...
And if that looks okay, you can actually run the changeset by:
liquibase --changeLogFile=“sample.xml” update -DSCHEMA=lookup
Is it possible to create two preconditons for single elements of a changeset? Like generating a column if the changelog file is run at a SQL Server database and generating another column if the changelog file is run at an Oracle database. In a way like this for example:
<changeSet author="Me" id="1528876614155">
<createTable tableName="ELECTRICITY_PRODUCTS">
<preConditions onFail="MARK_RAN" onSqlOutput="TEST">
<dbms type="mssql" />
<column autoIncrement="true" name="EP_ID" type="NUMBER">
</preConditions>
<preConditions onFail="MARK_RAN" onSqlOutput="TEST">
<dbms type="oracle" />
<column autoIncrement="false" name="EP_ID" type="FLOAT">
</preConditions>
</createTable>
</changeset>
It's not possible to create column inside condition.
If I understand your problem, then you have two (maybe more) options:
Separte the changeSets for each dbms
Create property for dbms and use them like:
<property dbms="oracle" name="autoincrement" value="true" />
<property dbms="oracle" name="autoincrementType" value="NUMBER" />
<property dbms="mssql" name="autoincrement" value="false" />
<property dbms="mssql" name="autoincrementType" value="FLOAT" />
<changeSet id="create_a_table" author="system">
<createTable tableName="a_table">
<column name="a_column" autoIncrement="${autoincrement}" type="${autoincrementType}" />
</createTable>
</changeSet>
I didn't tested that, it's just idea how to solve the problem.
This seems to be not possible but another good solution is to create changesets for these single elements. In this example it could be:
<changeSet author="Me" id="1528876614155">
<createTable tableName="DAILY DATA">
<column name="ID" type="NUMBER"/>
</createTable>
</changeset>
<changeSet author="Me" id="1528876614156">
<preConditions onFail="MARK_RAN" onSqlOutput="TEST">
<dbms type="oracle" />
</preConditions>
<addColumn tableName="DAILY_DATA">
<column name="VALUE" type="NUMBER"/>
</addColumn>
</changeset>
<changeSet author="Me" id="1528876614157">
<preConditions onFail="MARK_RAN" onSqlOutput="TEST">
<dbms type="mssql" />
</preConditions>
<addColumn tableName="DAILY_DATA">
<column name="VALUE" type="NUMBER"/>
</addColumn>
</changeset>
I am using a bastardised version of T-SQL to generate reports about information within a database driven CAD software (Solidworks Electrical). I am trying to generate a Table of Contents. Due to limitations within the software, I have to generate this table using SQL.
What I would like to do is create the Table of Contents in Excel, save it as a .csv, and have my SQL query read this file and spit it out as an output.
Example Table:
Sheet,System
1,Radios
2,Processors
3,Navigation
After some searching I've been unable to find a solution myself. My problems are:
1) Read a .csv file stored on my harddrive
2) Turn this .csv file into a table (cant get stored on the database, is just temporary while we run the query)
3) Output the data in this table as the results of the query
I have tried to use the following to read my .csv table, but recieve the error "Syntax error, permission violation, or other nonspecific error". So it's possible my software just won't allow me to read external files. (NB, my software uses ]] [[ instead of quotes....)
select
]]col1[[,
]]col2[[,
]]col3[[
from openrowset('MSDASQL'
,'Driver={Microsoft Access Text Driver (*.txt, *.csv)}'
,'select * from D:\SQL Queries\input.CSV')
Any assistance would be much appreciated! Thanks
This sql works for me:
select * from openrowset (bulk N'C:\Temp\source.csv', formatfile = N'C:\Temp\format.xml', firstrow=2) SourceFile
Content of source.csv is this:
Sheet,System
1,Radios
2,Processors
3,Navigation
Content of format.xml is this:
<?xml version="1.0"?>
<BCPFORMAT xmlns="http://schemas.microsoft.com/sqlserver/2004/bulkload/format" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<RECORD>
<FIELD ID="1" xsi:type="CharTerm" TERMINATOR=","/>
<FIELD ID="2" xsi:type="CharTerm" TERMINATOR="\r\n" MAX_LENGTH="128" COLLATION="SQL_Latin1_General_CP1_CI_AS"/>
</RECORD>
<ROW>
<COLUMN SOURCE="1" NAME="ID" xsi:type="SQLINT"/>
<COLUMN SOURCE="2" NAME="Name" xsi:type="SQLNVARCHAR"/>
</ROW>
</BCPFORMAT>
I have multiple schemas (2) and when generating the tables it is not able to generate FKs between the tables. All FKs are within one schema. When I remove the FKs it generates just fine.
I run it as
mvn clean liquibase:update -Dt1Schema=t1 -Dt2Schema=t2
What I've tried
<changeSet id="0" author="admin">
<createTable tableName="t1" remarks="t1" schemaName="${t1Schema}">
<column name="id" type="BIGINT" autoIncrement="true" remarks="id">
<constraints primaryKey="true" nullable="false"/>
</column>
</createTable>
<createTable tableName="t2" remarks="t2" schemaName="${t1Schema}">
<column name="id" type="BIGINT" autoIncrement="true" remarks="id">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="t1_id" type="BIGINT" remarks="t1 id">
<constraints nullable="true" deferrable="true" foreignKeyName="fk_t1_t2" referencedTableName="${t1Schema}.t1" referencedColumnNames="id"/>
</column>
</createTable>
</changeSet>
I get:
ERROR: relation "teeregister.t1" does not exist [Failed SQL: CREATE TABLE teeregister.t2 ...
<changeSet id="0" author="admin">
<createTable tableName="t1" remarks="t1" schemaName="${t1Schema}">
<column name="id" type="BIGINT" autoIncrement="true" remarks="id">
<constraints primaryKey="true" nullable="false"/>
</column>
</createTable>
<createTable tableName="t2" remarks="t2" schemaName="${t1Schema}">
<column name="id" type="BIGINT" autoIncrement="true" remarks="id">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="t1_id" type="BIGINT" remarks="t1 id">
<constraints nullable="true"/>
</column>
</createTable>
</changeSet>
<changeSet id="1" author="admin">
<addForeignKeyConstraint baseColumnNames="t1_id" baseTableName="${t1Schema}.t2" constraintName="fk_t1_t2" referencedColumnNames="id" referencedTableName="${t1Schema}.t1"/>
</changeSet>
I get:
ERROR: relation "teeregister.t2" does not exist [Failed SQL: ALTER TABLE "teeregister.t2" ADD CONSTRAINT ...
pom.xml configuration
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<changeLogFile>src/main/resources/schema1.xml</changeLogFile>
<changeLogFile>src/main/resources/schema2.xml</changeLogFile>
<driver>org.postgresql.Driver</driver>
<url>jdbc:postgresql://localhost:5432/postgres</url>
<username>postgres</username>
<password>postgres</password>
</configuration>
</plugin>
All I needed to do is read some documentation...
This works just fine
<addForeignKeyConstraint baseColumnNames="t1_id" baseTableSchemaName="${t1Schema}" baseTableName="t2" constraintName="fk_t1_t2" referencedColumnNames="id" referencedTableSchemaName="${t1Schema}" referencedTableName="t1"/>
I understand dropwizard expects all DB migrations to be in migrations.xml.
Let's say I'm starting a new project and create my tables:
<changeSet id="1" author="codahale">
<createTable tableName="people">
<column name="id" type="bigint" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="fullname" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="jobtitle" type="varchar(255)"/>
</createTable>
</changeSet>
At the next iteration I have to add a new column so I ad another changeSet to migrations.xml:
<changeSet id="2" author="dbdemopostgres">
<addColumn tableName="people">
<column name="description" type="varchar(255)">
<constraints nullable="false"/>
</column>
</addColumn>
</changeSet>
Then let's say I have to make a column longer so I add:
<changeSet id="3" author="dbdemopostgres">
<modifyDataType tableName="people" columnName="description" newDataType="varchar(300)"/>
</changeSet>
And so it goes. I keep appending all changes to my migrations.xml which grows indefinitely. On a large project it's just a matter of time when it becomes unmanagable. Does anyone has any recommendations for strategy to maintain ongoing database changes?
You can divide your migrations.xml file to multiple files as documented here apparently.
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<include file="com/example/db/changelog/db.changelog-1.0.xml"/>
<include file="com/example/db/changelog/db.changelog-1.1.xml"/>
<include file="com/example/db/changelog/db.changelog-2.0.xml"/>
</databaseChangeLog>