SQL Linked Server Notes (including to an IP)

Create linked server when on the actual server, not as a user on another PC.
If there is a VPN connection required then the VPN connection needs to be up and running on the server, not the local PC.

USE [master]
GO
EXEC master.dbo.sp_dropserver @server=N'', @droplogins='droplogins'
GO
EXEC master.dbo.sp_addlinkedserver @server = N'', @srvproduct=N'SQL Server'
GO
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'',@useself=N'False',@locallogin=NULL,@rmtuser=N'RemoteServerLogin',@rmtpassword=''
EXEC master.dbo.sp_addlinkedsrvlogin @rmtsrvname=N'',@useself=N'False',@locallogin='Domain\login',@rmtuser=N'RemoteServerLogin',@rmtpassword=''

.

Notes:
@locallogin=NULL means any local login
You cannot use Windows NT group logins, does not work. Individual window users only.

SQL Linked Server and SQL Server Agent

When using SQL Server Agent to do a job that has a view or other that connects to a remote server then you may get the following issues:

Error “Linked servers cannot be used under impersonation without a mapping for the impersonated login”

Solution 1:

  1. Create a login on the end server as:
    USE [master]
    GO
    CREATE LOGIN [NT SERVICE\SQLSERVERAGENT] FROM WINDOWS WITH DEFAULT_DATABASE=[master], DEFAULT_LANGUAGE=[British]
    GO
  2. Then give this login the permissions on the objects required.
  3. Back on the host right-click on the remote server > Properties > Security >
  4. Add “Local server login to remote server login mappings:”
    Add “NT SERVICE\SQLSERVERAGENT” Impersonate = ticked on – No remote user or password
    Suggest use “Be made using the login’s current security context”

Solution 2:

  1. Create a new SQL server login on the end server and give it the required permissions
  2. Back on the host right-click on the remote server > Properties > Security >
  3. Add “Local server login to remote server login mappings:”
    Add “NT SERVICE\SQLSERVERAGENT” Impersonate = ticked off – Remote user = your new user, Password = new password
    Suggest use “Be made using the login’s current security context”

 

T-SQL FileExist

File Exits


/*
SELECT dbo.myFileExist ('c:\Temp\Test.txt') AS FileExists
SELECT dbo.myFileExist ('c:\Temp\Test1.txt') AS FileExists
*/

ALTER FUNCTION dbo.myFileExist (@FileName varchar(100))
RETURNS int
AS
BEGIN
DECLARE @FileExists int
EXECUTE master.dbo.xp_fileexist @FileName, @FileExists OUT
RETURN @FileExists
END

Determine if a SQL table column has any unicode

Trying to reduce database-table-size by changing nvarchar to varchar where there is no unicode

http://answers.microsoft.com/en-us/ie/forum/ie9-windows_vista/only-secure-content-is-displayed/b5b31a5b-0b6f-4b6d-bf28-741ad30e9f37

SELECT TheColumn
FROM MyTable
WHERE TheColumn<>CONVERT(nvarchar(100),CONVERT(varchar(100),TheColumn))

SQL determine table row size

Personal note: See my SQL Monitor project


SELECT SCHEMA_NAME(so.schema_id) AS schemaname, OBJECT_NAME (sc.[id]) tablename
, COUNT (1) nr_columns
, SUM (sc.length) maxrowlength
FROM syscolumns sc
join sys.all_objects so on sc.[id] = so.[object_id]
--join sys.schemas s on so.schema_id = s.schema_id
WHERE so.is_ms_shipped = 0
GROUP BY so.schema_id, OBJECT_NAME (sc.[id])
ORDER BY SUM (sc.length) desc


SELECT SCHEMA_NAME(so.schema_id) AS schemaname, OBJECT_NAME (sc.[id]) tablename
, COUNT (1) nr_columns
, SUM (sc.length) maxrowlength
FROM syscolumns sc
join sys.all_objects so on sc.[id] = so.[object_id]
--join sys.schemas s on so.schema_id = s.schema_id
WHERE so.is_ms_shipped = 0
AND OBJECT_NAME(sc.id) = 'Orders'
GROUP BY so.schema_id, OBJECT_NAME (sc.[id])
ORDER BY SUM (sc.length) desc


dbcc showcontig ('Production.Orders') with tableresults

Table space used


EXEC sp_spaceused 'Product.Operations'

Make changes to an SQL Server Table without table re-creation

 

http://www.mssqltips.com/sqlservertip/1903/best-practices-for-sql-server-database-alter-table-operations/

 

Cannot generate SSPI context when connecting to SQL Server when a VPN is connected

We setup a VPN connection to another site, but then programs would not connect to SQL Server using Windows Authentication. We spent a fair bit of time trying to resolve this. Then found the solution, thanks to the following post:


http://www.sevenforums.com/network-sharing/59414-win7-vpn-sql-server-cannot-generate-sspi-context.html

After a lot of searching i found a good solution to the problem when using Windows login to a SQL server while on Win VPN giving the error: Cannot generate SSPI context.
Wanted to share it:

  1. Locate the .pbk file that contains the entry that you dial. Is either: C:\Users\<USERNAME>\AppData\Roaming\Microsoft\Network\Connections\Pbk or if connection is visible to all users: C:\ProgramData\Microsoft\Network\Connections\Pbk (hidden files may need to be disabled first through Organize -> Folder and search options)
  2. Open the file in Notepad using Open with…
  3. Locate the following entry: UseRasCredentials=1 Each VPN connection you have has a section marked with [<VPN connection name>]
  4. Modify the entry to the following: UseRasCredentials=0 Needs to be done for each VPN section for the connections having problems.
  5. On the File menu, click Save, and the click Exit.

Restart results in reseed of identity by +1000

http://connect.microsoft.com/SQLServer/feedback/details/739013/failover-or-restart-results-in-reseed-of-identity

Copied from

http://i.ll.do/2013/01/how-to-solve-sql-server-2012-identity.htm

How to – Solve SQL Server 2012 Identity Problem. Identity increased by 1000 or 10000

Are you experiencing an identity problem with your SQL Server 2012? Do the identity seeds of your records get increased by 1000 or 10000(in my case it was 10000)? Do you wonder why this is happening and how you can solve this? Then keep reading…

Seems like Microsoft has changed the way they deal with identity values in SQL Server 2012 and as a result of this you can see identity gaps between your records after rebooting your SQL server instance or your server machine. There might be some other reasons for this id gaps, but in my case it was an automatic server restart after installing an update.
There are several ways to overcome this issue but here I will explain how I did it, which seemed to be the best way of overcoming this problem.
Below is a quote from a Microsoft post replying to this id crisis issue on “connect”:
If you require the same identity generation semantics as previous versions of SQL Server there are two options available:
•         Use trace flag 272
o This will cause a log record to be generated for each generated identity value. The performance of identity generation may be impacted by turning on this trace flag.
•         Use a sequence generator with the NO CACHE setting(http://msdn.microsoft.com/en-us/library/ff878091.aspx)
o This will cause a log record to be generated for each generated sequence value. Note that the performance of sequence value generation may be impacted by using NO CACHE.
What I did: Setting Trace Flag 272 on SQL Server 2012
  1. Open “SQL Server Configuration Manager”
  2. Click “SQL Server Services” on the left pane
  3. Right-click on your SQL Server instance name on the right pane ->Default: SQL Server(MSSQLSERVER)
  4. Click “Properties”
  5. Click “Startup Parameters”
  6. On the “specify a startup parameter” textbox type “-T272”
  7. Click “Add”
  8. Confirm the changes
Now you should be free of this annoying identity gap problem.

Hope this helps someone.
Good luck, Serdar.

l

SSRS Login failed for unattended execution account

SOLVED

> Start > All Programs > Reporting Services Configuration Manager

> Connect

> Execution Account

> Put in new Account + Password

SSIS Troubleshooting

SSIS is case-sensitive and will remember the old column name even if it no longer exists. So say you have a column “CHANGE” and map it through. Then next time you use “Change” then SSIS will try to map “CHANGE” and throw errors. Try changing the column name to something completely different and then map through.