Errors Dropping a Index in Python - sql-server

I have these pymmsql index errors I created 3 indexes in my code and I want to drop them but they wouldn't drop I get these errors
Code:
DbConnect = 'Micros'
myDbConn = pymssql.connect(*******,"******", "*******",DbConnect)
cursor = myDbConn.cursor()
cursor.execute("""DROP INDEX [IF EXISTS]
Micros ON payrolldata;""")
cursor.execute("""DROP INDEX [IF EXISTS]
MainSort ON s20data,StoreSort ON s20data;""")
myDbConn.commit()
Error:
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Python37-32\SqlVersionpr_import.py", line 840, in proceed
Micros ON payrolldata;""")
File "src\pymssql.pyx", line 465, in pymssql.Cursor.execute
pymssql.ProgrammingError: (102, b"Incorrect syntax near 'Micros'.DB-Lib error message 20018, severity
15:\nGeneral SQL Server error: Check messages from the SQL Server\n")

Correct syntax would be.
DROP INDEX IF EXISTS IndexName;
DROP INDEX IndexName;
Square brackets mean that parameter or part of statement is optional.
See documentation
https://learn.microsoft.com/en-us/sql/t-sql/statements/drop-index-transact-sql?view=sql-server-ver15

Related

Trace file name is invalid

I am trying to copy the trace file information to SQL table with fn_trace_gettable function, but while running this function I am getting 'Trace file name is invalid'. And I confirmed the path and file name are correct and the default trace is enabled. Following is my SQL code, what I am doing wrong?
USE TestDB
SELECT * INTO [dbo].[traceTable]
FROM ::fn_trace_gettable(N'‪D:\MAT25.trc', default)
Error Message:
Msg 19050, Level 16, State 6, Line 11
Trace file name '‪‪D:\MAT25.trc' is invalid.
The statement has been terminated.

How to log multiple errors in TRY..CATCH?

My application runs SQL scripts to load in data and when there's a problem with the file it's loading, it only logs the last error which doesn't contain the useful information about why the file won't load.
Example code:
BEGIN TRY
BULK INSERT MyTableName
FROM 'C:\MyFilename.txt'
WITH
(
FIELDTERMINATOR = '|',
ROWTERMINATOR = '\n',
TABLOCK,
MAXERRORS=0,
ERRORFILE = 'C:\MyFilename_Errors.log'
)
;
END TRY
BEGIN CATCH
INSERT INTO MyErrorLog
SELECT ERROR_MESSAGE() as Issue
, ERROR_LINE() as IssueRowNum
;
END CATCH
This script will create an entry in [MyErrorLog] for the third error (see below). And the log file will tell me what line, but not what field:
Row 30539 File Offset 1910820 ErrorFile Offset 0 - HRESULT 0x80004005
Here's all 3 lines if I "THROW" the error inside the CATCH:
Msg 4864, Level 16, State 1, Line 3 Bulk load data conversion error
(type mismatch or invalid character for the specified codepage) for
row 1, column 5 (MyField).
Msg 7399, Level 16, State 1, Line 3 The OLE DB provider "BULK" for
linked server "(null)" reported an error. The provider did not give
any information about the error.
Msg 7330, Level 16, State 2, Line 3 Cannot fetch a row from OLE DB
provider "BULK" for linked server "(null)".
How do I capture that first (and second) error message?
You can only capture a single error in a T-SQL CATCH block. According to this connect item, the workaround is to use THROW (or not use TRY/CATCH) and capture the errors in client code.
Consequently, you'll need to invoke the script from a client application (including SQLCLR) so that you can capture and log all errors.

database python3 dimension error

c.execute('''CREATE TABLE IF NOT EXISTS Electronic
(elem_no INTEGER PRIMARY KEY, Econf text)''')
lists = []
for i in range(1,5):
filenm = str(i)+".html"
print(filenm)
vals = []
with open(filenm, "r") as elfile:
for line in elfile:
mstr = "]Electron Configuration"
if mstr in line:
vals.insert(len(vals),"Hello")
print(len(vals))
lists.append(vals)
print(lists)
c.executemany("INSERT INTO Electronic VALUES(?)",lists)
conn.commit()
where in each [1-5].html, i have a line:
\grep "Electron Configuration" 1.html
[186]Electron Configuration 1s^1
Now, the problem is, with this exact setup, I am getting error:
1.html
1
2.html
1
3.html
1
4.html
1
[['Hello'], ['Hello'], ['Hello'], ['Hello']]
Traceback (most recent call last):
File "elem_parse.py", line 134, in <module>
c.executemany("INSERT INTO Electronic VALUES(?)",lists)
sqlite3.OperationalError: table Electronic has 2 columns but 1 values were supplied
As I have never done database before, so I tried with:
c.executemany("INSERT INTO Electronic VALUES(?,?)",lists)
i.e. increasing no of field in VALUES, which is then giving error:
1.html
1
2.html
1
3.html
1
4.html
1
[['Hello'], ['Hello'], ['Hello'], ['Hello']]
Traceback (most recent call last):
File "elem_parse.py", line 134, in <module>
c.executemany("INSERT INTO Electronic VALUES(?,?)",lists)
sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 2, and there are 1 supplied.
Since I have never done database befor, I am following Python-sqlite3 - Auto-increment of not null primary key?
but now, I am lost and cant figure out the problem.
When you use two parameter markers (?), you also need to specify two values in each element of the lists list. But in this application, you do not actually want to specifiy different values (or any value at all).
With two columns in the table, you either have to give values for all columns:
INSERT INTO Electronic VALUES (NULL, ?);
or specifiy the columns you want to use:
INSERT INTO Electronic(Econf) VALUES (?);

How to set SQLException Number

I'm having an issue on settin up SqlException.Number
On my Stored Proc i'm raising an error
--#number = 50001
RAISERROR(#number, 16, 1) -
I should expect that the Error_Number() should be #number but I always get 18054
Is there something wrong with my RAISERROR?
Check the sys.messages table for error code 74601. if this is a user defined error, it shouold be added in the table.
for any error that is greater than 50000 should give you this output if not found.
Msg 18054, Level 16, State 1, Line 1
Error XXXXX, severity 16, state 1 was raised, but no message with that error number was found in sys.messages. If error is larger than 50000, make sure the user-defined message is added using sp_addmessage.
There is one small caveat: You can't supply a message on your own in this case. But this can be circumvented by adding an additional %s in the sp_addmessage call or by changing all mapped messages to your own pattern and supplying the right parameters in the raiseerror call.
Check there for more information:
SQL Server: Rethrow exception with the original exception number
RAISERROR can either reference a user-defined message stored in the
sys.messages catalog view or build a message dynamically.
Check for your error message exists or not using this:
select * from sys.messages
If it does not exists then Use sp_addmessage to add user-defined error messages and sp_dropmessage to delete user-defined error messages.
for more information follow RaiseError documentation.

SQL Server error

I am getting a fatal error on following code.
exec [sp_ExternalApp_UPDATEUSER] 'ZZZ', XXXXX','DDDDD','DDDFFDD','EREE', 'EREWWWWW',1,1,'QWEW#DFEE.DER','DEFF','XXXX','DDDD'
Following error occurred:
Location: memilb.cpp:1624
Expression: pilb->m_cRef == 0
SPID: 79
Process ID: 2256
Msg 3624, Level 20, State 1, Procedure sp_ExternalApp_UPDATEUSER, Line 32
A system assertion check has failed. Check the SQL Server error log for details
Msg 0, Level 20, State 0, Line 0
A severe error occurred on the current command. The results, if any, should be discarded.
Thank you
I got the solution.
I have used UPPER() function. As I removed that function, my problem solved
Like the message said, check your error log, there might be more detail in there..what does this proc do does it use sp_OACreate or calls xp_cmdshell? Post the proc code
You might want to check the database for corruption. Try
DBCC CHECKDB
(before doing so, read the documentation on that command! See http://msdn.microsoft.com/en-us/library/ms176064.aspx )

Resources