Skip to content

Instantly share code, notes, and snippets.

@Cyberloki
Cyberloki / example.js
Created September 22, 2013 21:04
Welcome to your first Gist! Gists are simple code reminders. Whenever you come across a piece of code you think might be useful later on, save it as a Gist. With GistBox, you can also tag the Gist with a label. This is especially useful for keeping them organized by language, project or purpose. For more info about GistBox, visit: http://www.gi…
// log an object to the browser console
console.log({ text: "foobar" });
@Cyberloki
Cyberloki / Create Tables and Indexes and Constraints.sql
Created September 22, 2013 21:39
Create Tables and Indexes and Constraints
if (object_id('Mobility_In_Queue') IS NOT NULL)
drop table [Mobility_In_Queue]
GO
-- ***********************************************
-- new table Mobility_In_Queue that holds the
-- data/messages uploaded from the mobile devices
-- ***********************************************
IF NOT EXISTS (select 1 from sysobjects where name = 'Mobility_In_Queue' and [type] = 'U')
create table [dbo].[Mobility_In_Queue] (
@Cyberloki
Cyberloki / Alter Table Add Field with Constraint.sql
Created September 22, 2013 21:40
Alter Table Add Field with Constraint
if not exists (select 1 from syscolumns where name = 'Integrated_Mapping' and ID in (select so.ID from sysobjects so where so.name = 'PRODUCTS' and so.type = 'U') )
alter table PRODUCTS ADD Integrated_Mapping bit NULL
GO
UPDATE PRODUCTS SET Integrated_Mapping = 0 where (Integrated_Mapping IS NULL)
GO
if not exists ( select 1 from sysconstraints where constid in
(select so.ID from sysobjects so
@Cyberloki
Cyberloki / Query SP_Who2.sql
Created September 22, 2013 21:43
Query SP_Who2 Who is accessing particular database programname and login
DECLARE @coffee TABLE (
spid int
, [status] varchar(50)
, [Login] varchar(255)
, HostName varchar(255)
, BlkBy varchar(50)
, [DBName] varchar(255)
, [Command] varchar(255)
, CPUTime int
, DiskIO int
@Cyberloki
Cyberloki / Query SP_Who.sql
Created September 22, 2013 21:44
Query SP_Who Who is accessing particular database - shows loginname
DECLARE @coffee TABLE (
spid int
, ecid int
, [status] varchar(50)
, loginame varchar(255)
, hostname varchar(255)
, blk varchar(50)
, dbname varchar(255)
, cmd varchar(255)
, request_id int
@Cyberloki
Cyberloki / Find Large MSSQL Database tables by tablesize.sql
Created September 22, 2013 21:46
Find Large MSSQL Database tables by tablesize
SELECT
t.NAME AS TableName,
p.rows AS RowCounts,
SUM(a.total_pages) * 8 AS TotalSpaceKB,
SUM(a.used_pages) * 8 AS UsedSpaceKB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
@Cyberloki
Cyberloki / Search all stored procedures and functions etc for specific text.sql
Created September 22, 2013 21:49
Search all stored procedures and functions etc for specific text
select so.name, sc.[text]
from syscomments sc with (nolock)
inner join sysobjects so with (nolock) on so.[id] = sc.[id]
where [text] like '%create_job%'
/* raw
select * from syscomments sc with (nolock) where [text] like '%create_job%'
*/
@Cyberloki
Cyberloki / select into_Insert into.sql
Created September 22, 2013 21:53
Create a new table dynamically and insert records (Select Into) Copy records into existing table (Insert Into)
-- create a new table and insert records
-- =====================================
select * into newtablename from MyFirstTable
-- copy records into existing table
-- =====================================
insert into ReprocessTable(event_time, [msg]) select event_time, [msg] from FailedTable order by [id]
-- create a new table with records from another database
@Cyberloki
Cyberloki / SQL AsXMLAttribute function.sql
Created September 22, 2013 22:51
SQL AsXMLAttribute function
if object_id('AsXMLAttribute') IS NOT NULL
DROP FUNCTION [dbo].[AsXMLAttribute]
GO
CREATE FUNCTION [dbo].[AsXMLAttribute] (
@text varchar(max)
)
RETURNS varchar(max)
AS
BEGIN
@Cyberloki
Cyberloki / Check if SQLDataReader has field or field is null
Created September 22, 2013 23:05
C# Check if SQLDataReader has field or field is null
// assumes dr is a SqlDataReader
List<string> columns_ = GetColumns(dr);
if (columns_.IndexOf("MissingField") == -1) { throw new Exception("Query did not contain \"MissingField\""); }
this.DBID = (int)dr["ID"];
this.Created = (DateTime)dr["Created"];
this.DataText = (string)dr["MissingField"];
if (!dr.IsDBNull(columns_.IndexOf("MsgType")) { this.MsgType = (string)dr["MsgType"]; }