Friday, March 15, 2019

T-SQL to find IP Address





SELECT CONNECTIONPROPERTY('net_transport') AS net_transport, CONNECTIONPROPERTY('protocol_type') AS protocol_type, CONNECTIONPROPERTY('auth_scheme') AS auth_scheme, CONNECTIONPROPERTY('local_net_address') AS local_net_address, CONNECTIONPROPERTY('local_tcp_port') AS local_tcp_port, CONNECTIONPROPERTY('client_net_address') AS client_net_address



Wednesday, February 27, 2019

PostgreSQL CamelCase problem

Given an OO language in which the usual naming convention for object properties is camelCased, and an example object like this:
{
    id: 667,
    firstName: "Vladimir",
    lastName: "Horowitz",
    canPlayPiano: true
}
How should I model this structure in a PostgreSQL table?
There are three main options:
  1. unquoted camelCase column names
  2. quoted camelCase column names
  3. unquoted (lowercase) names with underscores
They each have their drawbacks:
  1. Unquoted identifiers automatically fold to lowercase. This means that you can create a table with a canPlayPiano column, but the mixed case never reaches the database. When you inspect the table, the column will always show up as canplaypiano - in psql, pgadmin, explain results, error messages, everything.
  2. Quoted identifiers keep their case, but once you create them like that, you will always have to quote them. IOW, if you create a table with a "canPlayPiano" column, a SELECT canPlayPiano ... will fail. This adds a lot of unnecessary noise to all SQL statements.
  3. Lowercase names with underscores are unambiguous, but they don't map well to the names that the application language is using. You will have to remember to use different names for storage (can_play_piano) and for code (canPlayPiano). It also prevents certain types of code automation, where properties and DB columns need to be named the same.
So I'm caught between a rock and a hard place (and a large stone; there are three options). Whatever I do, some part is going to feel awkward. For the last 10 years or so, I've been using option 3, but I keep hoping there would be a better solution.

Friday, February 22, 2019

T-SQL to JSON

you need to use a varchar(max) else SSMS will limit it to 2033 chars

---------

declare @FSLI_JSON as varchar(max) = (

    SELECT [col1]
, [col2]
, [col3]...

    FROM [dbo].[myTable]
    FOR JSON auto
)

SELECT @myJSON

Monday, October 30, 2017

db Backup and Restore....to make life easier

--############################################################

--Step 1. :: backup the target database

--Step 2. :: Kill connections from the source database
use mastergo
DECLARE @kill varchar(8000) = '';SELECT @kill = @kill + 'kill ' + CONVERT(varchar(5), spid) + ';'FROM master..sysprocesses WHERE dbid = db_id('Reference Data Warehouse')

EXEC(@kill);
--Step 3. :: Single User Mode
ALTER Database [Reference Data Warehouse] SET Single_UserGO
--Step 4. :: Disconnect the session!

--Step 5. :: Multi-User Mode
ALTER Database [Reference Data Warehouse] SET Multi_User
GO
--##############################################################

EXEC sp_change_users_login 'UPDATE_ONE','MDMDQuser','MDMDQuser'


--First, make sure that this is the problem. This will lists the orphaned users:
EXEC sp_change_users_login 'Report'
--If you already have a login id and password for this user, fix it by doing:
EXEC sp_change_users_login 'Auto_Fix', 'user'
--If you want to create a new login id and password for this user, fix it by doing:
EXEC sp_change_users_login 'Auto_Fix', 'user', 'login', 'password'





Wednesday, October 11, 2017

Thursday, October 5, 2017

my_permissions



USE [sandbox]
GO

select * from sys.fn_my_permissions(NULL, 'database')

select * from sys.fn_my_permissions(NULL, 'server')

Friday, May 12, 2017

Quick View Table Structure and Contents

Viewing table Structure & Contents
USE [db_name]
go

declare @tableName as nvarchar(50) = 'test'
declare @SQLstmt as nvarchar(100) = 'SELECT top 10* FROM [dbo].[' + @tableName + ']'

SELECT obj.type_desc,  OBJECT_NAME(col.object_id) as 'table_name', col.column_id, col.name as 'column_name'
, TYPE_NAME(col.user_type_id) as 'DataType_name'
--, col.system_type_id, col.user_type_id
, col.max_length, col.[precision], col.scale, col.collation_name, col.is_nullable, col.is_ansi_padded, col.is_rowguidcol, col.is_identity
FROM sys.objects obj inner join sys.columns col on obj.object_Id=col.object_Id
WHERE obj.Name=@tableName
--AND col.name LIKE '%org%'
ORDER BY col.column_id

EXEC sp_executesql @SQLstmt

Monday, May 8, 2017

Month and Day Leading Zeros? 09 not 9

declare @REL_START_DATE as datetime = GetDate()
SELECT cast(year(@REL_START_DATE) as varchar(4)) + '-' + RIGHT('0' + cast(month(@rel_start_date) as varchar(2)),2) + '-' + right('0' + cast(day(@rel_start_date) as varchar(2)),2) + ' 00:00:00.000' as 'REL_START_DATE'

Friday, March 31, 2017

Always On

If secondary runs out of space, make sure the Always On connection is removed and re-added so that no data is lost between the two.