how to send values to snowflake procedures array_construct field - sqlalchemy

EXECUTION
try:
session = self.get_session()
session.execute("BEGIN TRANSACTION;")
if with_result:
output = session.execute(query, values)
result = output.mappings().all()
session.execute("commit;")
print("result = ", result)
return result
FUNCTION
def insert_data(self, data ):
y = list(data)
values = {'data_assets': [y]}
query = "call data_table(:data_assets);"
self.database.execute_query(query,values)
PROCEDURE
create or replace data_table(data_val array)
returns Table()
language sql
{code}
VALUE PASSED TO THE FUNCTION
k = {"path":["CATEGORY","PERSONAL ","EMAIL","PERSONAL_MAIL","EMAIL"],"id":1,"size":50,"pers_id": 081945}', '{"path":["CATEGORY","PERSONAL ","EMAIL","PERSONAL_MAIL","PHONE_NO"],"id":1,"size":70,"pers_id": 081945}', '{"path":["CATEGORY","PERSONAL ","EMAIL","MANAGER","USER"],"id":1,"size":40,"pers_id": 081945} ', '{"path":["CATEGORY","PERSONAL ","EMAIL","PERSONAL_MAIL","VIN"],"id":1,"size":120,"pers_id": 081945}', ' {"path":["CATEGORY","PERSONAL ","EMAIL","PERSONAL_MAIL","USER"],"id":1,"size":500,"pers_id": 081945} ', ' {"path":["CATEGORY","PII","EMAIL","PERSONAL_MAIL","USER"],"id":1,"size":20,"pers_id": 081945}', '{"path":["CATEGORY","PERSONAL ","EMAIL","EMPLOYEE","USER"],"id":1,"size":70,"pers_id": 081945} ', ' {"path":["CATEGORY","PERSONAL ","EMAIL","PERSONAL_MAIL","US_PHONE_NO"],"id":1,"size":80,"pers_id": 081945}', ' {"path":["CATEGORY","PERSONAL ","EMAIL","PERSONAL_MAIL","LOCATION"],"id":1,"size":5,"pers_id": 081945}', ' {"path":["CATEGORY","PERSONAL ","EMAIL","ADDRESS","USER"],"id":1,"size":90,"pers_id": 081945}', ' {"path":["CATEGORY","PERSONAL ","EMAIL","PERSONAL_MAIL","ADMIN"],"id":1,"size":1,"pers_id": 081945} '
ERROR:
sqlalchemy.exc.ProgrammingError: (snowflake.connector.errors.ProgrammingError) 001003 (42000): SQL compilation error:
syntax error line 1 at position 59 unexpected '"path"'.
syntax error line 1 at position 76 unexpected '['.
syntax error line 1 at position 87 unexpected ','.
syntax error line 1 at position 134 unexpected ']'.
[SQL: call data_table(%(data_assets)s);]
[parameters: {'data_assets': [['{"path":["CATEGORY","PERSONAL ","EMAIL","PERSONAL _MAIL","EMAIL"],"id":1,"size":50,"pers_id": 081945}', '{"datasour ... (1200 characters truncated) ... 081945}', ' {"path":["CATEGORY","PERSONAL ","EMAIL","PERSONAL _MAIL","ADMIN"],"id":1,"size":1,"pers_id": 081945} ']]}]
not able to pass parameter to array field(in procedures)

You can pass a list as an Array Constant. I don't know how one can use binding variables.
ary = list(k)
query = f"call proc({ary})"
self.database.execute_query(query)

Related

How to remove a parenthesis from a cellvalue in sql?

I have a cell value (-4.00), where i want to replace the parenthesis '(', ')' with space or nothing at all, but it gives me error.
I tried Replace(Replace(cellvalue, ')', ' '),'(', ' ') but the error that I get is
Number value '' is not recognised.
You could use TRANSLATE:
SELECT cellvalue,
TRANSLATE(cellvalue::STRING, '()', ' ') AS paranthesis_removed,
TRY_CAST(paranthesis_removed AS NUMBER) -- if cast needed
FROM Tab

How do I fetch data from a database, in MATLAB, with where clause using "and" operator?

conn= database ('test', ' ' , ' ');
sql = ' SELECT * from table1 WHERE column1= value1 and column2 = value2';
results = fetch(conn, sql);
results
This piece of code gives output as follows
results = [ ]

Using ltrim and rtrim in combination of substring

Hey there so I'm in the process of another project conversion there is a line written in Oracle SQL and I'm trying to convert it to MS SQL:
Oracle PL/SQL:
IF LTRIM(sCmtStr) IS NOT NULL THEN
sTrimStr := ' '||SUBSTR(RTRIM(LTRIM(sCmtStr),'; '),1,999);
ELSE
sTrimStr := NULL;
MS T-SQL:
IF ltrim(#sCmtStr) IS NOT NULL
SET #sTrimStr = ' ' + ISNULL(substring(rtrim(ltrim(#sCmtStr), '; '), 1, 999), '')
ELSE
SET #sTrimStr = NULL
I get the following error:
Msg 174, Level 15, State 1, Procedure TRIMCOMMENT, Line 12
The rtrim function requires 1 argument(s).
Any idea? Thank you.
RTRIM (https://learn.microsoft.com/en-us/sql/t-sql/functions/rtrim-transact-sql) Does not take 2 arguments; it can only trim spaces from the end of a string.
TRIM (https://learn.microsoft.com/en-us/sql/t-sql/functions/trim-transact-sql), on the other hand, can be given other characters to remove. It will remove the characters from both ends, however, so you might have to fiddle a bit if you only want it to remove from the end of the string.

Postgresql recordset output display in Ruby cucumber

I am facing difficulty in displaying postgresql output in ruby and also if there is any way to print the same output in html.
Given below is the code and Error:
Then(/^selecting source table$/) do
require 'pg'
conn = PGconn.connect("localhost",5433,'','', "db", "postgres", "pass")
result = conn.exec('select EMP_ID,DEPT_ID,DEPT_NAME,EMP_NAME from EMP_TABLE')
result.each do |row|
puts row['EMP_ID'] + ' | ' + row['DEPT_ID'] + ' | ' + row['DEPT_NAME'] + ' | ' + row['EMP_NAME']
end
end
ERROR:
NoMethodError: undefined method +' for nil:NilClass
./features/step_definitions/my_steps.rb:45:inblock (2 levels) in ' ./features/step_definitions/my_steps.rb:44:in each'
./features/step_definitions/my_steps.rb:44:in/^selecting source table$/' ./features/PG.feature:12:in `Then selecting source table' 1 scenario (1 failed) 1 step (1 failed) 0m0.072s
One of your row[whatever] values is nil. If you cast everything to a string it should work.
result.each do |row|
puts row['EMP_ID'].to_s + ' | ' + row['DEPT_ID'].to_s + ' | ' + row['DEPT_NAME'].to_s + ' | ' + row['EMP_NAME'].to_s
end
Even better, you can use the .join() method to do that and clean up your logic at the same time.
result.each do |row|
puts row.join(' | ')
end
EDIT!
My bad, that's a hash, not an array. Give me 10 minutes and I'll give you the correct answer...
EDIT 2
Since it's a hash, you need to add the .values method in there.
result.each do |row|
puts row.values.join(' | ')
end

Format the SQL query to a variable

DECLARE #sql nvarchar(4000)
SET #sql='SELECT DISTINCT
WS.SIMNumber,
SMTP.SMTPMappingId,
CONVERT(VARCHAR(11),WS.ExpiryDate,106) as ExpiryDate,
CASE
WHEN BES.DisplayName IS NOT NULL THEN BES.DisplayName+'#'+SMTP.DomainName
ELSE
CASE WHEN BES.PIN IS NOT NULL THEN BES.PIN+'#'+SMTP.DomainName
ELSE '' END END AS EmailId,
CASE
WHEN (SELECT COUNT(*) FROM dbo.ExpiringEmailSimCardSent WHERE SimNumber=WS.SIMNumber AND ExpiryDate=WS.ExpiryDate)>0
THEN CONVERT(BIT,1)
ELSE CONVERT(BIT,0)
END AS IsEMailSent
FROM
WEBSERVICE_CACHE AS WS
LEFT OUTER JOIN
BES_SERVER_CACHE AS BES ON WS.SIMNumber = LEFT(BES.ICCID,19)
LEFT OUTER JOIN
BES_SMTP_Mapping AS SMTP ON BES.BESSqlServer = SMTP.BesServer
WHERE
CONVERT(DATETIME, GETDATE(), 109) > CONVERT(DATETIME, WS.ExpiryDate, 109)'
EXECUTE sp_executesql #sql
I have this SQL query want to convert it into a `nvarchar` variable because of # and '' . I am getting some errors
I am getting this errorMsg 102, Level 15, State 1, Line 9
Incorrect syntax near '#'.
If I rectify that it comes for another at # and ' '
You have to 'escape' your quotes:
This:
BES.PIN+'#'+SMTP.DomainName
Should be something like this:
BES.PIN+'''#'''+SMTP.DomainNam
experiment....