Tag Archive for sql

Search stored procedures for string

SELECT
ROUTINE_NAME,
ROUTINE_DEFINITION
FROM
INFORMATION_SCHEMA.ROUTINES
WHERE
ROUTINE_DEFINITION LIKE '%whatev%'
ORDER BY
ROUTINE_NAME

source

TSQL Function and Check Constraint to Ensure that a Parent Value is Present in the Table

ALTER TABLE [dbo].[Message]  WITH CHECK ADD  CONSTRAINT [CK_Message] CHECK  (([InReplyToId] IS NULL OR [dbo].[CheckInReplyToMessageId]([InReplyToId])>=(0)))
GO
ALTER TABLE [dbo].[Message] CHECK CONSTRAINT [CK_Message]

CREATE FUNCTION [dbo].[CheckInReplyToMessageId]
(
@MessageId BigInt
)
RETURNS bit
AS
BEGIN
DECLARE @RetVal bit

SET @RetVal = (SELECT MessageId
FROM Message
WHERE MessageId = @MessageId)

RETURN isnull(@RetVal, 0)

END

source

Create SQL OR ID List

function createIDList($idList, $dbFieldName) {
if(count($idList) == 0) return '';

$orString = '';

foreach($idList as $id) {
$orString .= "{$dbFieldName} = '{$id}' OR ";
}

return substr($orString, 0, -4);
}

// ex:
$query = "SELECT * FROM table_name WHERE ".createIDList(array(array('id_field' => 1), array('id_field' => 2)));

source

Count Rows in Sql

Person.count(:conditions => "age > 26")

source

Change owner on sql server db.

Change owner on sql server db.

Restore database from .bak file

create login
give dbo role.

Run the script:

use db
go

SELECT ‘EXEC(”sp_changeobjectowner @objname = ””’+
ltrim(u.name) + ‘.’ + ltrim(s.name) + ”””
+ ‘, @newowner = NEWOWNER”)’
FROM sysobjects s,
sysusers u
WHERE s.uid = u.uid
AND u.name ‘NEWOWNER’
AND xtype in (‘V’, ‘P’, ‘U’, ‘FN’)
AND u.name not like ‘INFORMATION%’
order by s.name

copy paste result to query analyzer

go

Delete old user

exec sp_changedbowner ‘newowner’

Change login name for dbo to newowner (pwd = same)
bmssa/bmssa

source

Drop down option in field for MySQL

Campo ENUM ('-','D','E','L','S') NOT NULL

source

Ordenar un query por longitud de una cadena

SELECT ... ORDER BY LENGTH(your_field);

source

Find a constraint by name

SELECT *
FROM all_constraints
WHERE constraint_name='THE_CONSTRAINT';

source

TSQL Backup / Restore

BACKUP DATABASE @dbname
TO DISK = @filelocation
GO

RESTORE DATABASE @dbname
FROM DISK = @filelocation
GO

source

Program used to compare the SAS datasets in two directories

Here is the new Proc compare.sas program, I have developed ....to compare all the datasets in 2 directories(testing and production) at once and to quick check any mismatches.
Proc compare only check if there is any mismatches between the datasets in 2 directories. If any, it reports otherwise it will give us a note saying that:

Note: No unequal Values were found. All values compared are exactly equal.

contd.....

source