snowflake get list of table given the database - snowflake-cloud-data-platform

I am using snowflake jdbc driver to get the list of tables in a given database,
i have 2 prepared statement. I get the PreparedStatement from the same Connection
PreparedStatement usePrepStatement = con.prepareStatement("use database IDENTIFIER(:1)");
usePrepStatement.setString(1, database);
usePrepStatement.execute();
and to get the tables from information_schema
String sql = "select table_schema, \n" +
" table_name, \n" +
" table_type \n" +
"from information_schema.tables where table_catalog = '?'";
PreparedStatement preparedStatement = con.prepareStatement(sql);
preparedStatement.setString(1, database);
ResultSet tableResultset = preparedStatement.executeQuery();
Here tableResultset is empty. How to I get this query to return the right result
I can query the current_database() I see the current database is set to the right value
The above queries works if i use Statement instead of PreparedStatement.

Identifiers are stored in uppercase, so using case insensitive comparison should solve it.Also there is no need to wrap the parameter with ':
from information_schema.tables where table_catalog = '?'
=>
from information_schema.tables where table_catalog ILIKE ?

Related

MultiLine Query in Snowflake Stored Procedure

I am trying to build one stored procedure in snowflake which will
i. Delete existing records from dimension table.
ii. Insert the records into the dimension table from corresponding materialized view.
iii. Once the data is inserted, record the entry into a config table.
First two are running fine, third one is not working, code below
Table DIM_ROWCOUNT
CREATE TABLE ANALYTICSLAYER.AN_MEDALLIA_P.DIM_ROWCOUNT
(
"TABLE_NAME" VARCHAR(500),
"VIEW_NAME" VARCHAR(500),
"TABLE_ROWCOUNT" VARCHAR(500) ,
"VIEW_ROWCOUNT" VARCHAR(500),
"LOADDATE" timestamp
)
The SP has parameter as SRC_TABLE_NAME which should be loaded for column : TABLE_NAME,
VIEW_NAME will be derived inside code.
TABLE_ROWCOUNT and VIEW_ROWCOUNT will be calculated within the code.
I need to have a multi line query for insertion.
How to create the insert statement multiline?
var config_details = `INSERT INTO DBNAME.SCHEMANAME.TABLENAME
SELECT '${VAR_TABLE_NAME}','${VAR_VIEW_NAME}','${VAR_TABLE_ROW_COUNT}','${VAR_VIEW_ROW_COUNT}',getdate();`
var exec_config_details = snowflake.createStatement( {sqlText: config_details} );
var result_exec_config_details = exec_config_details.execute();
result_exec_config_details.next();
Any help in appreciated.
I tend to write complex SQL statements in a javascript SP by appending each line/portion of text to the variable, as I find it easier to read/debug
Something like this should work (though I haven't tested it so there may be typos). Note that to get single quotes round each value I am escaping them in the code (\'):
var insert_DIM_ROWCOUNT = 'INSERT INTO DBNAME.TEST_P.DIM_ROWCOUNT ';
insert_DIM_ROWCOUNT += 'SELECT \'' + SRC_TABLE_NAME + '\', \'' + VIEW_NAME + '\', \'';
insert_DIM_ROWCOUNT += TABLE_ROWCOUNT + '\', \'' + VIEW_ROWCOUNT + '\', ';
insert_DIM_ROWCOUNT += GETDATE();
I then test this to make sure the SQL statement being created is what I want by adding a temporary line of code, after this, to just return the SQL string:
return insert_DIM_ROWCOUNT;
Once I'm happy the SQL is correct I then comment out/delete this line and let the SQL execute.

SQL Server Linked Server openquery for WHERE clause

I am trying to write a query to get the delta between my server and Linked Server. So I wrote the following which worked until:
Select MD5 FROM ServerA A
WHERE NOT EXISTS
(
SELECT 1
FROM [LinkedServer].[Database].[dbo].[Files] fi
WHERE A.MD5High = fi.MD5High AND A.MD5Low = fi.MD5Low
)
However this pulls all the data from LinkedServer to my server which causes my server to run out of resources. So my next attempt was to perform the query on the linked server
Select MD5 FROM ServerA A
WHERE NOT EXISTS
(
SELECT 1
FROM OPENQUERY( [LinkedServer] , 'SELECT [LinkedServer].[Database].[dbo].[Files] fi
WHERE fi.MD5High = ' + A.MD5Low + ' AND fi.MD5Low= ' + a.MD5Low + '')
)
However the syntax in the second query is incorrect and I can't figure the correct syntax.
I appreciate any direction

Able to access data through pyodbc and SELECT statements, but no new tables show up in SQL

I have the following code which is working with no errors and returning the expected output when I print the results of the pyodbc cursor I created.
cnxn = pyodbc.connect(MY_URL)
cursor = cnxn.cursor()
cursor.execute(
'''
CREATE TABLE tablename(
filename VARCHAR(100),
synopsis TEXT,
abstract TEXT,
original TEXT,
PRIMARY KEY (filename)
)
'''
)
for file in file_names_1:
try:
query = produce_row_query(file, tablename, find_tag_XML)
cursor.execute(query)
except pyodbc.DatabaseError as p:
print(p)
result = cursor.execute(
'''
SELECT filename,
DATALENGTH(synopsis),
DATALENGTH(abstract),
original
FROM ml_files
'''
)
for row in cursor.fetchall():
print(row)
However, no new tables are showing up in my actual MS SQL server. Am I missing a step to push the changes or something of that nature?
You need to commit changes or else they will not be updated in your actual database.
cnxn.commit()

Groovy, safe way to create MSSQL database

I'm trying to use groovy.sql.Sql to create databases in an MSSQL (Microsoft SQL Server) server. It seem like the prepared statement adds additional quotes around the last parameter breaking the query.
This test code:
import groovy.sql.Sql
import com.microsoft.sqlserver.jdbc.SQLServerDataSource
def host = 'myhost'
def port = '1433'
def database = 'mydatabasename'
def usernameName = 'myusername'
def password = 'mypassword'
def dataSource = new SQLServerDataSource()
dataSource.setURL("jdbc:sqlserver://$host:$port")
dataSource.setUser(username)
dataSource.setPassword(password)
def connection new Sql(dataSource)
connection.execute(
'IF EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE name = ?) DROP DATABASE ?',
[ databaseName, databaseName ]
)
Gives the error:
Failed to execute: IF EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE name = ?) DROP DATABASE ? because: Incorrect syntax near '#P1'.
How can I use prepared statements without having it add single quotes around parameter one (DROP DATABASE ? seem to be rewritten as DROP DATABASE '?') or can I write the query in a different way so that the added single quotes does not produce a syntax error?
I would also be fine with other frameworks, if anyone could give me a working example.
Can you try:
connection.execute(
"IF EXISTS (SELECT * FROM master.dbo.sysdatabases WHERE name = $databaseName) DROP DATABASE ${Sql.expand(databseName)}"
)

Display file path of documents in Crystal Report

I have a crystal report that lists invoices and I would like to be able to show the file path of the invoices in another column of the report. The following query allows me to search for the documents based on their unique ID number. It then displays the file location of the one document I search for, however I would like to have this apply to all of the documents listed in the report. Can someone please help me out with this?
`DECLARE #entryID INT = 35793
SELECT dbo.toc.name AS DocumentName, dbo.doc.pagenum + 1 AS PageNum, dbo.vol.fixpath + '\' +
SUBSTRING(CONVERT(VARCHAR(8),CONVERT(VARBINARY(4), dbo.doc.storeid),2),1,2) + '\' +
SUBSTRING(CONVERT(VARCHAR(8),CONVERT(VARBINARY(4), dbo.doc.storeid),2),3,2) + '\' +
SUBSTRING(CONVERT(VARCHAR(8),CONVERT(VARBINARY(4), dbo.doc.storeid),2),5,2) + '\' +
CONVERT(VARCHAR(8),CONVERT(VARBINARY(4), dbo.doc.storeid),2) + '.TIF' AS FullPathAndFilename
FROM dbo.doc
LEFT JOIN dbo.toc ON dbo.doc.tocid = dbo.toc.tocid
LEFT JOIN dbo.vol ON dbo.toc.vol_id = dbo.vol.vol_id
WHERE dbo.doc.tocid = #entryID
ORDER BY dbo.doc.pagenum`
The answer depends on how you currently retrieve data in the existing Crystal Report.
Option A: If your report data source is a "Command" (in other words you have written a SQL statement in the crystal report that retrieves the data you need), then you will want to modify that command to pull in this new information via a subquery. So for example if your current report SQL is something like "select x.* from foo as x", you would want it to be something like:
select x.*,
(SELECT dbo.vol.fixpath + '\'
+ SUBSTRING(CONVERT(VARCHAR(8),CONVERT(VARBINARY(4), dbo.doc.storeid),2),1,2) + '\'
+ SUBSTRING(CONVERT(VARCHAR(8),CONVERT(VARBINARY(4), dbo.doc.storeid),2),3,2) + '\'
+ SUBSTRING(CONVERT(VARCHAR(8),CONVERT(VARBINARY(4), dbo.doc.storeid),2),5,2) + '\'
+ CONVERT(VARCHAR(8),CONVERT(VARBINARY(4), dbo.doc.storeid),2)
+ '.TIF' AS FullPathAndFilename
FROM dbo.doc
LEFT JOIN dbo.toc ON dbo.doc.tocid = dbo.toc.tocid
LEFT JOIN dbo.vol ON dbo.toc.vol_id = dbo.vol.vol_id
WHERE dbo.doc.tocid = x.tocid //this line joining the new SQL to all report rows
)
from foo as x;
Option B: If instead you pull your data from Crystal using the join wizard, then it would probably be best to create a new view in your database that matches the SQL you provided (minus the last 2 lines), and then join your existing main report tables to this view in the Crystal join wizard.

Resources