TSQL - Bulk Insert - tsql

BULK
INSERT Table1
FROM 'C:\\Table1.txt' --location with filename
WITH
.
.
.
The Table1.txt is on a Windows Server. How should I write the WITH-Statement (Connection String)?

Try this:
BULK INSERT
Table1
FROM
'\\fullpath\Table1.txt'
WITH
(FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
Please Note: SQL Server NT service should have access to the specified address, otherwise you will get the following error:
Operating system error code 5(Access is denied.)

FROM '\\SERVER\\FOLDER\\FOLDER\\FileName.txt'
That's it.
Sorry.THX.

Related

Equivalent of PRAGMA table_info(table) for Postgres SQL?

I am working with a script made for SQLite but I need to adapt it to a PostgreSQL database and this line is always returning an error:
PRAGMA table_info(table)
How can I get an equivalent result on postgres >
Found it !
SELECT *
FROM information_schema.columns
WHERE table_name = 'table_name'

While unloading the table to s3 the database gets restarted

I'm trying to unload a table to S3, whenever I'm trying I would get below message.
FATAL: A fatal error occurred. The database will be restarted.
SSL SYSCALL error: EOF detected
The connection to the server was lost. Attempting reset: Failed.
query= UNLOAD (' select * from public.table ') to 's3://bucket/path/in/s3/' credentials 'aws_access_key_id=####;aws_secret_access_key=####' delimiter '\t' GZIP PARALLEL ON ALLOWOVERWRITE NULL AS 'M15sInGValue57ring' ESCAPE ADDQUOTES;
NULL AS string cannot be longer than 18 characters.
UNLOAD (' select * from public.table ') to 's3://bucket/path/in/s3/' credentials 'aws_access_key_id=####;aws_secret_access_key=####' delimiter '\t' GZIP PARALLEL ON ALLOWOVERWRITE NULL AS 'M15sInG57ring' ESCAPE ADDQUOTES;
It works !!!
Reason Why it Failed?
Whenever null encounters while unloading, it would replace with NULL
AS string.
There it expects it to be lesser than 18 characters.
Otherwise while converting, it would fail.
So that DB restart occurs.

Access SAP Tables in DB2

I have a DB2 database, where few of the tablenames are starting with '/'.
eg: /dev/r32
How can I query on the above table
I have tried with the following approaches
select * from schemaname./dev/r32
select * from schemaname.'/dev/r32'
select * from schemaname."/dev/r32"
But I am getting the following error:
An error occurred when executing the SQL command:
select * from schemaname."/dev/r32"
[SQL0204] /dev/r32 in schemaname type *FILE not found. [SQL State=42704, DB Errorcode=-204]
describe schemaname."/dev/r32" works.
Any help would be appreciated
The table names are changed and issue is resolved.

How to log an error into a Table from SQLPLus

I am very new to Oracle so please base with me if this is covered else where.
I have a MS SQL box running Jobs calling batch files running scripts in SQLPLUS to ETL to an Oracle 10G database.
I have an intermittent issue with a script that is causing the ETL to fail which at the minute without error logging is something of an unknown. The current solution highlights the load failure based on rowcounts for before and after the script has finsihed.
I'd like to be able to insert any errors encoutered whilst running the offending script into an error log table on the same database receiving the data loads.
There's nothing too technical about the script, at a high level is performs the following steps all via SQL code and no procedural calls.
Updates a table with Date and current row counts
Pulls data from a remote source into a staging table
Merges the Staging table into an intermediate staging table
Performs some transformational actions
Merges the intermediate staging table into the final Fact table
Updates a table with new row counts
Please advise whether it is possible to pass error messages, codes, line number etc etc via SQLPLUS into a Database table? and if so the easiest method to achieve this.
A first few lines of the script are shown below to give a flavour
/*set echo off*/
set heading off
set feedback off
set sqlblanklines on
/* ID 1 BATCH START TIME */
INSERT INTO CITSDMI.CITSD_TIMETABLE_ORDERLINE TGT
(TGT.BATCH_START_TIME)
(SELECT SYSDATE FROM DUAL);
COMMIT;
insert into CITSDMI.CITSD_TIMETABLE_ALL_LOADS
(LOAD_NAME, LOAD_CRITICALITY,LOAD_TYPE,BATCH_START_TIME)
values
('ORDERLINE','HIGH','SMART',(SELECT SYSDATE FROM DUAL));
commit;
/* Clear the Staging Tables */
TRUNCATE TABLE STAGE_SMART_ORDERLINE;
Commit;
TRUNCATE TABLE TRANSF_FACT_ORDERLINE;
Commit;
and so it goes on with the rest of the steps.
Any assistant will be greatly appreciated.
Whilst not fully understanding your requirement, a couple of pointers.
The WHENEVER command will help you control what sqlplus should do when an error occurs, e.g.
WHENEVER SQLERROR EXIT FAILURE ROLLBACK
WHENEVER OSERROR EXIT FAILURE ROLLBACK
INSERT ...
INSERT ...
This will cause sqlplus to exit with error status 1 if any of the following statements fail.
You can also have WHENEVER SQLERROR CONTINUE ...
Since the WHENEVER ... EXIT FAILURE/SUCCESS controls the exit status, the calling script/program will know if it worked failed.
Logging
use SPOOL to spool the out to a file.
Logging to table.
Best way is to wrap your statements into PLSQL anonymous blocks and use exception hanlders to log errors.
So, putting the above together, using a UNIX shell as the invoker:
sqlplus -S /nolog <<EOF
WHENEVER SQLERROR EXIT FAILURE ROLLBACK
CONNECT ${USRPWD}
SPOOL ${SPLFILE}
BEGIN
INSERT INTO the_table ( c1, c1 ) VALUES ( '${V1}', '${V2}' );
EXCEPTION
WHEN OTHERS THEN
INSERT INTO the_error_tab ( name, errno, errm ) VALUES ( 'the_script', SQLCODE, SQLERRM );
COMMIT;
END;
/
SPOOL OFF
QUIT
EOF
if [ ${?} -eq 0 ]
then
echo "Success!"
else
echo "Oh dear!! See ${SPLFILE}"
fi

How to programatically compare permissions of login/user in SQL Server 2005

There's a login/user in SQL Server who is having a problem importing accounts in production server. I don't have an idea what method he is doing this. According to the one importing, this import is working fine in development server. But when he did the same import in production it is giving him errors. Below are the errors he is getting for each accounts.
2009-06-05 18:01:05.8254 ERROR [engine-1038] Task [1038:00001 - Members]: Step 1.0 [<Insert step description>]: Task.RunStep(): StoreRow has failed
2009-06-05 18:01:05.9035 ERROR [engine-1038] Task [1038:00001 - Members]: Step 1.0 [<Insert step description>]: Task.RunStep(): StoreRow exception: Exception caught while storing Data. [Microsoft][ODBC SQL Server Driver][SQL Server]'ACCOUNT1' is not a valid login or you do not have permission.
Please note that 'ACCOUNT1' is not the real account name. I just changed it for security reason.
Using SQL Server Management Studio (SSMS), I viewed/checked the permissions of the user/login who is performing the import from development server and production for comparison. I found no difference.
My question is: Is there a way to programmatically query permissions in server and database level of a particular login/user so I can compare/contrast for any differences?
Red Gate's SQL Compare wil do it for you.
This code shows all the rights that the login has.
select sys.schemas.name 'Schema', sys.objects.name Object, sys.database_principals.name username, sys.database_permissions.type permissions_type,
sys.database_permissions.permission_name,
sys.database_permissions.state permission_state,
sys.database_permissions.state_desc,
state_desc + ' ' + permission_name + ' on ['+ sys.schemas.name + '].[' + sys.objects.name + '] to [' + sys.database_principals.name + ']' COLLATE LATIN1_General_CI_AS
from sys.database_permissions
join sys.objects on sys.database_permissions.major_id =
sys.objects.object_id
join sys.schemas on sys.objects.schema_id = sys.schemas.schema_id
join sys.database_principals on sys.database_permissions.grantee_principal_id =
sys.database_principals.principal_id
order by 1, 2, 3, 5
You should be able to use
sp_helplogins '<loginname>'
See the MSDN reference here.