-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path004_create_sp_for_access_browser.sql
64 lines (50 loc) · 2.05 KB
/
004_create_sp_for_access_browser.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
-------------------- SCRIPT TO CHECK OF DbScriptMigrationSystem -------------------------------
DECLARE @MigrationName AS VARCHAR(1000) = '004_create_sp_for_access_browser'
IF EXISTS(SELECT MigrationId FROM [DbScriptMigration] WHERE MigrationName = @MigrationName)
BEGIN
raiserror('MIGRATION ALREADY RUNNED ON THIS DB!!! STOP EXECUTION SCRIPT', 11, 0)
SET NOEXEC ON
END
INSERT INTO [DbScriptMigration]
(MigrationId, MigrationName, MigrationDate)
VALUES
(NEWID(), @MigrationName, GETDATE())
GO
PRINT 'Insert record into [DbScriptMigration]!'
-------------------- END SCRIPT TO CHECK OF DbScriptMigrationSystem ---------------------------
-------------------- SCRIPT TO CHECK PREREQUISITES OF DbScriptMigrationSystem -------------------------------
DECLARE @PrerequisiteMigrationName AS VARCHAR(1000) = '002_DropAndCreate_Tables'
IF NOT EXISTS(SELECT MigrationId FROM [DbScriptMigration] WHERE MigrationName = @PrerequisiteMigrationName)
BEGIN
raiserror('YOU HAVE TO RUN SCRIPT %s ON THIS DB!!! STOP EXECUTION SCRIPT ', 11, 0, @PrerequisiteMigrationName)
SET NOEXEC ON
END
-------------------- END SCRIPT TO CHECK PREREQUISITES OF DbScriptMigrationSystem ---------------------------
DECLARE @sqlString AS VARCHAR(MAX)
SET @sqlString = 'CREATE OR ALTER PROCEDURE sp_get_or_create_access_browser
@user_agent NVARCHAR(MAX)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @findId INT = NULL
-- retrieve id of user agent, if it exists
SELECT @findId = id FROM accesses_browsers WHERE user_agent_string LIKE @user_agent
-- if not exists, create it
IF (@findId) IS NULL
BEGIN
-- INSERT NEW ENTRY
INSERT INTO [accesses_browsers]
([user_agent_string])
VALUES
(@user_agent)
-- RETRIEVE LAST ID
SELECT @findId = SCOPE_IDENTITY()
END
SELECT @findId
END '
EXEC (@sqlString)
PRINT 'Stored procedure [sp_get_or_create_access_browser] created!'
---------------- FOOTER OF DbScriptMigrationSystem : REMEMBER TO INSERT -----------------------
SET NOEXEC OFF