Cannot access SqlTransaction object to rollback in catch block

using (var Conn = new SqlConnection(_ConnectionString)) { SqlTransaction trans = null; try { Conn.Open(); trans = Conn.BeginTransaction(); using (SqlCommand Com = new SqlCommand(ComText, Conn, trans)) { /* DB work */ } trans.Commit(); } catch (Exception Ex) { if (trans != null) trans.Rollback(); return -1; } } or you could go even cleaner and easier and … Read more

Truncate table in Oracle getting errors

You have to swap the TRUNCATE statement to DELETE statements, slower and logged but that’s the way to do it when constraints are in place. DELETE mytablename; Either that or you can find the foreign keys that are referencing the table in question and disable them temporarily. select ‘ALTER TABLE ‘||TABLE_NAME||’ DISABLE CONSTRAINT ‘||CONSTRAINT_NAME||’;’ from … Read more

Django equivalent of COUNT with GROUP BY

If you are using Django 1.1 beta (trunk): Player.objects.values(‘player_type’).order_by().annotate(Count(‘player_type’)) values(‘player_type’) – for inclusion only player_type field into GROUP BY clause. order_by() – for exclusion possible default ordering that can cause not needed fields inclusion in SELECT and GROUP BY.

How can I avoid NULLs in my database, while also representing missing data?

Good on you, for eliminating Nulls. I have never allowed Nulls in any of my databases. Of course, if nulls are prohibited, then missing information will have to be handled by some other means. Unfortunately, those other means are much too complex to be discussed in detail here. Actually it is not so hard at … Read more

Splitting delimited values in a SQL column into multiple rows

If you are on SQL Server 2016+ You can use the new STRING_SPLIT function, which I’ve blogged about here, and Brent Ozar has blogged about here. SELECT s.[message-id], f.value FROM dbo.SourceData AS s CROSS APPLY STRING_SPLIT(s.[recipient-address], ‘;’) as f; If you are still on a version prior to SQL Server 2016 Create a split function. … Read more

Postgresql delete multiple rows from multiple tables

Arranging proper cascading deletes is wise and is usually the correct solution to this. For certain special cases, there is another solution to this that can be relevant. If you need to perform multiple deletes based on a common set of data you can use Common Table Expressions (CTE). It’s hard to come up with … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)