How to Replace Multiple Characters in SQL?

One useful trick in SQL is the ability use @var = function(…) to assign a value. If you have multiple records in your record set, your var is assigned multiple times with side-effects: declare @badStrings table (item varchar(50)) INSERT INTO @badStrings(item) SELECT ‘>’ UNION ALL SELECT ‘<‘ UNION ALL SELECT ‘(‘ UNION ALL SELECT ‘)’ … Read more

Query runs fast, but runs slow in stored procedure

OK, we have had similar issues like this before. The way we fixed this, was by making local parameters inside the SP, such that DECLARE @LOCAL_Contract_ID int, @LOCAL_dt_From smalldatetime, @LOCAL_dt_To smalldatetime, @LOCAL_Last_Run_Date datetime SELECT @LOCAL_Contract_ID = @Contract_ID, @LOCAL_dt_From = @dt_From, @LOCAL_dt_To = @dt_To, @LOCAL_Last_Run_Date = @Last_Run_Date We then use the local parameters inside the SP … Read more

Changing INT to BigInt

If one or more of those columns have no constraints against them (like a foreign key, index, default, rule, etc), you should be able to change each one quickly by doing ALTER TABLE monster ALTER COLUMN MyIntCol1 bigint Management Studio’s change SQL is rarely the most efficient and tends to favour temporary tables for anything … Read more

Change a Primary Key from Nonclustered to Clustered

1) Drop the existing clustered index first (IX_TableX_FieldB): DROP INDEX TableX.IX_TableX_FieldB 2) Create a (temporary) UNIQUE constraint on the unique fields referenced in the primary key ALTER TABLE TableX ADD CONSTRAINT UQ_TableX UNIQUE(FieldA) 3) Drop the PRIMARY KEY ALTER TABLE TableX DROP CONSTRAINT PK_TableX 4) Recreate the PRIMARY KEY as CLUSTERED ALTER TABLE TableX ADD … Read more

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