Skip to content

Instantly share code, notes, and snippets.

View xivSolutions's full-sized avatar

John Atten xivSolutions

View GitHub Profile
@xivSolutions
xivSolutions / gist:9281856
Created February 28, 2014 23:05
currval() Issue in PG
-- A table, "turtles":
CREATE TABLE turtles
(
id serial NOT NULL,
name text,
lastid integer,
CONSTRAINT pk_turtles PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
@xivSolutions
xivSolutions / gist:9359513
Created March 5, 2014 01:33
Overriding Roles and adding JsonIgnore Attribute
public class ApplicationUser : IdentityUser
{
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
[Required]
public string Email { get; set; }
@xivSolutions
xivSolutions / gist:9520848
Created March 13, 2014 02:25
PG Select Specific Serial Sequence
SELECT c.relname, c.relkind FROM pg_class c WHERE c.relkind = 'S' and relname = 'album_albumid_seq';
@xivSolutions
xivSolutions / gist:9520925
Created March 13, 2014 02:31
PG get last value of sequence
SELECT last_value FROM album_album_id_seq
@xivSolutions
xivSolutions / gist:9542109
Last active August 29, 2015 13:57
PG Get Serial Sequence - determine if PG table column has an associated sequence
select COUNT(pg_get_serial_sequence('"clientdocuments"', 'ClientDocumentId')) As CountAuto
@xivSolutions
xivSolutions / gist:9542115
Created March 14, 2014 04:22
SQL Server columnproperty - determine if SQL Server Column is identity column
SELECT columnproperty(object_id('Album'),'AlbumId','IsIdentity') AS IsIdentity
@xivSolutions
xivSolutions / gist:9583088
Created March 16, 2014 13:19
SQL Server Table -> Identity Columns
select COLUMN_NAME, TABLE_NAME
from INFORMATION_SCHEMA.COLUMNS
where TABLE_SCHEMA = 'dbo'
and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
order by TABLE_NAME
@xivSolutions
xivSolutions / gist:9583365
Last active August 29, 2015 13:57
SQL Server - Key Columns, with PK and IsIdentity Status
SELECT tc.TABLE_NAME, kcu.COLUMN_NAME, kcu.CONSTRAINT_NAME, tc.CONSTRAINT_TYPE,
(COLUMNPROPERTY(object_id(tc.TABLE_NAME), kcu.COLUMN_NAME, 'IsIdentity')) AS IsIDColumn
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
ON kcu.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA AND kcu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
@xivSolutions
xivSolutions / gist:9583742
Last active August 29, 2015 13:57
SQL Server - All Columns with type and key usage
SELECT c.TABLE_NAME, c.COLUMN_NAME, kcu.CONSTRAINT_NAME, c.DATA_TYPE, c.CHARACTER_MAXIMUM_LENGTH, tc.CONSTRAINT_TYPE,
CASE tc.CONSTRAINT_TYPE WHEN 'PRIMARY KEY' THEN CAST(1 AS BIt) ELSE CAST(0 AS Bit) END AS IsPrimaryKey,
CASE (COLUMNPROPERTY(object_id(tc.TABLE_NAME), kcu.COLUMN_NAME, 'IsIdentity')) WHEN 1 THEN CAST(1 AS BIT) ELSE CAST(0 AS Bit) END as IsAuto
FROM INFORMATION_SCHEMA.COLUMNS c
LEFT OUTER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
ON c.TABLE_SCHEMA = kcu.CONSTRAINT_SCHEMA AND c.TABLE_NAME = kcu.TABLE_NAME AND c.COLUMN_NAME = kcu.COLUMN_NAME
LEFT OUTER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
ON kcu.CONSTRAINT_SCHEMA = tc.CONSTRAINT_SCHEMA AND kcu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
@xivSolutions
xivSolutions / gist:9593309
Last active August 29, 2015 13:57
PG Determine if Column is Auto-Incrementing serial ID
SELECT (CASE (
(SELECT CASE (LENGTH(pg_get_serial_sequence('artist', 'artist_id')) > 0) WHEN true THEN 1 ELSE 0 END) +
(SELECT CASE (SELECT pgc.relkind FROM pg_class pgc WHERE pgc.relname = 'artist' || '_' || 'artist_id' || '_' || 'seq') WHEN 'S"' THEN 1 ELSE 0 END))
WHEN 0 THEN false ELSE true END) AS IsAuto