Use TRUNCATE TABLE against SQL Server in Power Query? - sql-server

I'd like to use Microsoft Power Query to truncate a SQL Server table.
I wrote the M-Query code below, using the technique in Power BI write back to sql source:
let
Source = Sql.Database("server_host/instance_name", "database_name"),
Sql = "truncate table [target_table]",
RunSql = Value.NativeQuery(Source, Sql)
in
RunSql
When I run this, it fails and gives the error message "Expression.Error: This native database query isn't currently supported."
Is it possible to execute the TRUNCATE TABLE statement in Power Query against SQL Server, and if so, how?

Try this:
let
Source = Sql.Database("server_host/instance_name", "database_name"),
Sql = "truncate table [target_table] select 1",
RunSql = Value.NativeQuery(Source, Sql)
in
RunSql

Related

pyodbc insert into SQL Server db table stopped committing. Why?

I have a python script that has been inserting into a SQL Server table for a few weeks since I wrote it. Suddenly it stopped inserting and I can see that it looks like a COMMIT problem because the Primary Key Identity ID column in the table increments - if I do a T-SQL insert in SSMS, I can see that several ID values have been skipped. The rows seem to be inserted but are rolled back by the look of it. I've restarted the SQL Server instance and restarted the VS Code app I am using to run the script from. No success. No errors from python/pyodbc. I've run out of ideas. Any suggestions?
import pyodbc
SQL_DRIVER = 'SQL Server Native Client 11.0'
SQL_OUTPUT_TABLE = "test"
SERVER = "myServer"
DATABASE = "myDB"
def main():
cnxn = pyodbc.connect('DRIVER={'+SQL_DRIVER+'};SERVER='+SERVER+';DATABASE='+DATABASE+';Trusted_Connection=yes')
cursor = cnxn.cursor()
tsql : str = "insert into [dbo].[test](col1) values ('stuff');"
cursor.execute(tsql)
cursor.commit
cnxn.close
if __name__ == '__main__':
main()
Tried alternative SQL drivers. Created the test script you see here to reduce the scope as much as possible. Service restarts, etc. Can also successfully insert rows directly from within SSMS to the table.
I think I found the problem. cursor.execute followed by conn.commit fixed it.
import pyodbc
SQL_DRIVER = 'SQL Server Native Client 11.0'
SQL_OUTPUT_TABLE = "test"
SERVER = "DESKTOP-GBCJUII"
DATABASE = "xen_mints"
conn = pyodbc.connect('Driver={'+SQL_DRIVER+'};'
'Server='+SERVER+';'
'Database='+DATABASE+';'
'Trusted_Connection=yes;')
cursor = conn.cursor()
cursor.execute('''
INSERT INTO '''+SQL_OUTPUT_TABLE+''' (col1)
VALUES
('stuff')
''')
conn.commit()

Incorrect syntax near Go with Pypyodbc

I am using the pypyodbc library to establish a connection to a SQL Server 2008 R2 database and every time I try to execute a .sql file I encounter the following error:
pypyodbc.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect syntax near 'Go'.")
This is the sql query I am trying to execute:
Use SL_Site1_App
Go
select emp_num,name, trans_num, job, trans_type
from Hours where trans_type like '1000%' order by trans_date desc
This is the python script that I am using:
import pypyodbc, ExcelFile
def main():
# read the SQL queries externally
queries = ['C:\\Temp\\Ready_to_use_queries\\Connection_sql_python.sql']
for index, query in enumerate(queries):
cursor = initiate_connection_db()
results = retrieve_results_query(cursor, query)
if index == 0:
ExcelFile.write_to_workbook(results)
print("The workbook has been created and data has been inserted.\n")
def initiate_connection_db():
connection_live_db = pypyodbc.connect(driver="{SQL Server}", server="xxx.xxx.xxx.xxx", uid="my-name",
pwd="try-and-guess", Trusted_Connection="No")
connection = connection_live_db.cursor()
return connection
The workaround for this problem is to delete the Use SL_Site1_App Go line but I want to know if this is a known problem related to the pypyodbc library to process these lines and if so, where should I look to notify the developers about this issue.
GO is a batch separator used by sqlcmd and SSMS. It's not a T-SQL operator.
Considering you're using an application to connect to SQL Server, declare your database in the connection string, by adding database="SL_Site1_App", and then remove the USE and GO statements in your SQL Statement.

Run query on sql server through teradata and store result in teradata

I have one table in SQL server and 5 tables in Teradata.I want to join those 5 table in teradata with sql server table and store result in Teradata table.
I have sql server name but i dont know how to simultaneously run a query both on sql server and teradata.
i want to do this:
sql server table query
Select distinct store
from store_Desc
teradata tables:
select cmp_id,state,sde
from xyz
where store in (
select distinct store
from sql server table)
You can create a table (or a volatile table if you do not have write privileges) to do this. Export result from SQL Server as text or into the language of your choice.
CREATE VOLATILE TABLE store_table (
column_1 datatype_1,
column_2 datatype_2,
...
column_n datatype_n);
You may need to add ON COMMIT PRESERVE ROWS before the ; to the above depending on your transaction settings.
From a language you can loop the below or do an execute many.
INSERT INTO store_table VALUES(value_1, value_2, ..., value_n);
Or you can use the import from text using Teradata SQL Assistant by going to File and selecting Import. Then execute the below and navigate to your file.
INSERT INTO store_table VALUES(?, ?, ..., n);
Once you have inserted your data you can query it by simply referencing the table name.
SELECT cmp_id,state,sde
FROM xyz
WHERE store IN(
SELECT store
FROM store_table)
The DISTINCT function is most easily done on export from SQL Server to minimize the rows you need to upload.
EDIT:
If you are doing this many times you can do this with a script, here is a very simple example in Python:
import pyodbc
con_ss = pyodbc.connect('sql_server_odbc_connection_string...')
crs_ss = con_ss.cursor()
con_td = pyodbc.connect('teradata_odbc_connection_string...')
crs_td = con_td.cursor()
# pull data for sql server
data_ss = crs_ss.execute('''
SELECT distinct store AS store
from store_Desc
''').fetchall()
# create table in teradata
crs_td.execute('''
CREATE VOLATILE TABLE store_table (
store DEC(4, 0)
) PRIMARY INDEX (store)
ON COMMIT PRESERVE ROWS;''')
con_td.commit()
# insert values; you can also use an execute many, but this is easier to read...
for row in data_ss:
crs_td.execute('''INSERT INTO store_table VALUES(?)''', row)
con_td.commit()
# get final data
data_td = crs_td.execute('''SELECT cmp_id,state,sde
FROM xyz
WHERE store IN(
SELECT store
FROM store_table);''').fetchall()
# from here write to file or whatever you would like.
Is fetching data from the Sql Server through ODBC an option?
The best option may be to use Teradata Parallel Transporter (TPT) to fetch data from SQL Server using its ODBC operator (as a producer) combined with Load or Update operator as the consumer to insert it into an intermediate table on Teradata. You must then perform rest of the operations on Teradata. For the rest of the operations, you can use BTEQ/SQLA to store the results in the final Teradata table. You can also put the same SQL in TPT's DDL operator instead of BTEQ/SQLA and get it done in a single job script.
To allow use of tables residing on separate DB environments (in your case SQL-Server and Teradata) in a single select statement, Teradata has recently released Teradata Query Grid. But I'm not sure about exact level of support for SQL-Server and it will involve licensing hassle and quite a learning curve to do this simple job.

"The column cannot be modified because it is an identity, rowversion or a system column" - but it isn't

I am getting this error:
The column cannot be modified because it is an identity, rowversion or
a system column. [Column name = BatchClosed]
But [BatchClosed] is a nullable bit column and identity is false.
I am using Sql Server Compact Edition and the table is used in merge replication.
There are system columns ( _sysIG, _sysCG, _sysCD, _sysP1, _sysMC, _sysMCS, _sysSR) and a rowguid for the purpose of replication in the table.
The table is not marked as download-only in the publication.
The table is filtered though, and the BatchClosed field is used as a part of that filter:
WHERE surveyorid = convert(int, HOST_NAME()) AND BatchClosed = 0
When I test it in Management Studio connected to the Sql Server CE database with this sql I get the same error
UPDATE tblBatch SET BatchClosed = 0 WHERE BatchClosed = 1 AND SurveyID = 160;
Interestingly, this sql would not actually do an update because there are no records with BatchClosed = 1. (I assume that's just something to do with the way Sql Server CE works)
NB the test sql will work in Sql Server 2008 R2 but not on the Sql Server CE version after synchronization
EDIT
If I try to update any column in that table I get the same error message - as if all columns are system columns, not just the one in the filter
EDIT 2
I checked my installation and noted that the server tools had an older installation date while the x64 version was at SP1:
So I un-installed the x64 components, then downloaded and installed the server tools. It now looks like this:
I immediately lost my web synchronization. It took me a painful day of working through various dead ends before I found out how to get that back. (Solution here: Configuring Web Synchronization for Merge Replication to Sql Server CE)
Result? Still get the same error. :-(
I can both delete and insert in the table in question, and also update like this:
-- Script Date: 05-07-2014 09:26 - ErikEJ.SqlCeScripting version 3.5.2.39
UPDATE [tblBatch]
SET [SamplePercentage] = 0
WHERE BatchId = 2;
GO
I think you cannot update any other columns, as they are either system controlled (PK or rowguid) or participate in join filters in the publication. But to do updates, you can do a DELETE followed by an INSERT.

How to execute remote query by Oracle Database Link

I use Oracle Database Link to query data from SQL Server. The query is like:
select *
from tableA#DL_SqlServer a
join tableB#DL_SqlServer b
on a.ID = b.ID
tableA and tableB is large and the result is relatively small. This query executes quickly in SQL Server since indexes are built both on the two tables. But it is very slow on Oracle Database Link to SQL Server. I guess the join operation is performed on Oracle side not on SQL Server side, thus the indexes are not used. Since I just need the joined result, I prefer to perform the query entirely on SQL Server and get the small result only. I konw that using SQL Server's linked server and OPENQUERY function can achieve this goal. I wonder how to do this on Oracle Database Link. Thanks! Btw, I have no privilege to create views on SQL Sevrer.
You most likely need to use the DBMS_HS_PASSTHROUGH package. Something like
DECLARE
l_cursor PLS_INTEGER;
BEGIN
l_cursor := dbms_hs_passthrough.open_cursor#dblink_to_sql_server;
dbms_hs_passthrough.parse#dblink_to_sql_server( l_cursor, <<select statement>> );
while dbms_hs_passthrough.fetch_row#link_to_sql_server(l_cursor) > 0
loop
dbms_hs_passthrough.get_value#dblink_to_sqlserver( l_cursor, 1, <<local variable for first column>> );
dbms_hs_passthrough.get_value#dblink_to_sqlserver( l_cursor, 2, <<local variable for second column>> );
...
end loop;
dbms_hs_passthrough.close_cursor#dblink_to_sqlserver( l_cursor );
END;

Resources