Saturday, 23 August 2014

Windows: Ignore errors with Xcopy and RoboCopy

To copy entire directory structures as quickly as possible and ignore all disk errors (useful in data recovery) either of the following commands should work with robocopy being the quickest (if you’ve got Vista/7 or XP with the XP Resource Kit installed). Both commands use source -> destination path order.
xcopy /C/H/R/S/Y c:\ d:\

/C = Continues copying even if errors occur
/H = Copies hidden and system files also
/R = Overwrites read-only files
/S = Copies directories and subdirectories
/Y = Overwrites existing files without asking
robocopy c:\ d:\ /MIR /R:0 /W:0

/MIR = Mirror entire directory structure (can use /E instead)
/R:0 = 0 retries for read/write failures
/W:0 = 0 seconds between retries
Source: http://djlab.com/2010/12/windows-ignore-errors-with-xcopy-and-robocopy/

Monday, 14 July 2014

SQL get duplicates per column

SELECT
    name, email, COUNT(*)
FROM
    users
GROUP BY
    name, email
HAVING 
    COUNT(*) > 1

Thursday, 12 June 2014

Merging multiple excel files into one

Sub Macro1()
  Dim path As String, ThisWB As String, lngFilecounter As Long
    Dim wbDest As Workbook, shtDest As Worksheet, ws As Worksheet
    Dim Filename As String, Wkb As Workbook
    Dim CopyRng As Range, Dest As Range
    Dim RowofCopySheet As Integer

    RowofCopySheet = 2 ' Row to start on in the sheets you are copying from

    ThisWB = ActiveWorkbook.Name
   
    path = "C:\..."

    Application.EnableEvents = False
    Application.ScreenUpdating = False

    Set shtDest = ActiveWorkbook.Sheets(1)
    Filename = Dir(path & "\*.xls", vbNormal)
    If Len(Filename) = 0 Then Exit Sub
    Do Until Filename = vbNullString
        If Not Filename = ThisWB Then
            Set Wkb = Workbooks.Open(Filename:=path & "\" & Filename)
            Set CopyRng = Wkb.Sheets(1).Range(Cells(RowofCopySheet, 1), Cells(ActiveSheet.UsedRange.Rows.Count, ActiveSheet.UsedRange.Columns.Count))
            Set Dest = shtDest.Range("A" & shtDest.UsedRange.SpecialCells(xlCellTypeLastCell).Row + 1)
            CopyRng.Copy Dest
            Wkb.Close False
        End If
       
        Filename = Dir()
    Loop

    Range("A1").Select
   
    Application.EnableEvents = True
    Application.ScreenUpdating = True
   
    MsgBox "Done!"
End Sub

Wednesday, 11 June 2014

Wednesday, 4 June 2014

Running large SQL file from CMD

sqlcmd -S [servername] -d [databasename] -i [scriptfilename]


...remove square brackets above

Wednesday, 14 May 2014

Getting list of stored procedures and granting execute to a user

Get a list of stored procedures
select SPECIFIC_NAME  from [database_name].information_schema.routines where routine_type = 'PROCEDURE'

Granting the permission to execute specific stored procedure
GRANT EXECUTE ON [_TextGet] TO [some_user]

Granting the permission to execute all stored procedures
GRANT EXEC TO [some_user]
GO

Tuesday, 4 February 2014

SQL Insert query with returning inserted values

INSERT INTO dbo.YourTable(col1, col2, col3, ...., ColN) OUTPUT Inserted.Col1, Inserted.Col5, Inserted.ColN VALUES(val1, val2, val3, ....., valN)