import pyodbc as p
server = 'ServerName'
database = 'DatabaseName'
userid = 'UserName'
pwd = 'UserPassword'
connStr = ( r'DRIVER={SQL Server};SERVER=' +
server + ';DATABASE=' + database + ';' +
'UID=' + userid + ';PWD='+pwd+';')
lst = []
conn = p.connect(connStr)
dbCursor = conn.cursor()
sql = ('SELECT ColumnName AS FieldValue FROM tblTableName')
dbCursor = conn.cursor()
dbCursor.execute(sql)
for row in dbCursor:
lst.append(row.FieldValue)
conn.close()
print lst
Thursday, December 9, 2010
Read a SQL Server table into a list using Python + pyodbc
Labels: Python, SQL Server, SQL Server Tip
Friday, December 3, 2010
Shrinking Truncate Log File
USE YourDatabaseName
GO
--Run this script to get your TransactionLogName
SELECT * FROM sys.sysfiles
GO
DBCC SHRINKFILE(TransactionLogName, 1)
BACKUP LOG YourDatabaseName WITH TRUNCATE_ONLY
DBCC SHRINKFILE(TransactionLogName, 1)
GO
Labels: DBA Tasks, SQL Server, SQL Server Tip
Monday, October 18, 2010
How to kill session remotely
1. Download PsTools at this address http://technet.microsoft.com/en-us/sysinternals/bb896649.aspx and then run Windows command prompt at the folder containing PsTools
2. Run psexec command: D:\PsTools> psexec \\RemoteMachineName_Or_IPAddress -u UserName -p Password cmd
Wait a moment to allow to connect to remote server. A new command prompt will appear like that:
C:\WINDOWS\system32>
3. Run: qwinsta to get session ID need to log off: C:\WINDOWS\system32>qwinsta
A list of sessions will display, choose one you need to kill
4. Run: C:\WINDOWS\system32>logoff SessionID /v
Labels: DBA Tasks
Thursday, October 7, 2010
Create linked server to Access 2007 accdb file
EXEC master.dbo.sp_addlinkedserver @server = N'AccessLinkedServerName', @srvproduct=N'Access', @provider=N'Microsoft.ACE.OLEDB.12.0', @datasrc=N'D:\Projects\MyAccessDB.accdb'
GO
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname = N'AccessLinkedServerName', @locallogin = NULL , @useself = N'False'
Labels: Access, linked server, SQL Server, SQL Server Tip, T-SQL
Thursday, July 29, 2010
SET NUMERIC ROUNDABORT
What will be the output of the following scripts?
SET NUMERIC_ROUNDABORT ON SET ARITHABORT ON GO DECLARE @c NUMERIC(5, 2), @a NUMERIC(5, 4), @b NUMERIC(5, 4) SET @a = 1.1234 SET @b = 1.1234 SELECT @c = @a + @b SELECT @c
A. 2.24
B. 2.25
C. Error message: Arithmetic overflow error converting numeric to data type numeric.
D. 2.2468
Answer:[C]
Highlight to find out the answer.
Labels: SQL Server, SQL Server Test, SQL Server Tip
Tuesday, July 27, 2010
Get numbers from 1 to 999 with a query
WITH tblDigit(d) AS ( SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0) SELECT t1.d + t2.d * 10 + t3.d*100 AS Number FROM tblDigit t1, tblDigit t2, tblDigit t3 WHERE t1.d + t2.d + t3.d >0 ORDER BY t1.d + t2.d*10 + t3.d*100
Labels: SQL Server 2005, SQL Server Tip, T-SQL
Saturday, July 24, 2010
Defragment indexes
You are writing a new stored procedure to perform maintenance on your SQL Server 2005 databases that defragment the indexes in an online manner. What command should you use?
A. DBCC DBREINDEX
B. DBCC INDEXDEFRAG
C. ALTER INDEX with the REORGANIZE option
D. ALTER INDEX with the LOB_COMPACTION option set to OFF
Answer:[C]
Highlight to find out the answer.
Labels: DBA Tasks, SQL Server 2005, SQL Server Test, SQL Server Tip
Thursday, July 22, 2010
Logical join
Do you know the difference between logical join and physical join?
What are not logical join?
A. INNER JOIN
B. LEFT JOIN
C. RIGHT JOIN
D. MERGE JOIN
E. SELF JOIN
F. CROSS JOIN
Answer:[D]
Highlight to find out the answer.
Labels: SQL Server, SQL Server Test, SQL Server Tip
Page size again
Do you remember pages in SQL Server?
Your SQL Server database log file is 5MB. How many pages are allocated to it?
A. 0
B. 640
C. 320
D. 80
E. It depends
Answer:[A]
Highlight to find out the answer.
Labels: DBA Tasks, SQL Server, SQL Server Test, SQL Server Tip
Page size
Do you remember pages in SQL Server?
In SQL Server, the page size is _____
A. 1KB
B. 8KB
C. 16KB
D. 64KB
E. 125KB
F. 256KB
Answer:[B]
Highlight to find out the answer.
Labels: SQL Server, SQL Server Test, SQL Server Tip
