Sanitize table/column name in Dynamic SQL in .NET? (Prevent SQL injection attacks)

I’m not sure if you’re still looking into this, but the DbCommandBuilder class provides a method QuoteIdentifier for this purpose. The main benefits of this are that it’s database-independent and doesn’t involve any RegEx mess. As of .NET 4.5, you have everything you need to sanitize table and column names just using your DbConnection object: … Read more

User defined variables in PostgreSQL

Postgres does not normally use variables in plain SQL. But you can do that, too: SET foo.test=”SELECT bar FROM baz”; SELECT current_setting(‘foo.test’); Read about Customized Options in the manual. In PostgreSQL 9.1 or earlier you needed to declare custom_variable_classes before you could use that. However, You cannot EXECUTE dynamic SQL without a PL (procedural language). … Read more

Generate Delete Statement From Foreign Key Relationships in SQL 2008?

Here is a script for cascading delete by Aasim Abdullah, works for me on MS SQL Server 2008: IF OBJECT_ID(‘dbo.udfGetFullQualName’) IS NOT NULL DROP FUNCTION dbo.udfGetFullQualName; GO CREATE FUNCTION dbo.udfGetFullQualName (@ObjectId INT) RETURNS VARCHAR (300) AS BEGIN DECLARE @schema_id AS BIGINT; SELECT @schema_id = schema_id FROM sys.tables WHERE object_id = @ObjectId; RETURN ‘[‘ + SCHEMA_NAME(@schema_id) … Read more