Simple script to insert data
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
This is a simple script which inserts data into a couple of tables. I'm very new to SQL so any feeback, of any kind, would be much appreciated!
DECLARE
@Model_ID INT
, @ModelVersion VARCHAR(10)
, @ExistsInDM INT
, @IsActive BIT
SET @ModelVersion = '2.46.7' -- new version number
SET @IsActive = '1'
SELECT @Model_ID = Model_ID
FROM [sch_AM].[tblDMModelVersion] WITH (NOLOCK)
WHERE [ModelVersion] = @ModelVersion
IF @Model_ID IS NULL
BEGIN
INSERT INTO [sch_AM].[tblDMModelVersion]
( [ModelVersion]
,[IsActive]
)
VALUES ( @ModelVersion
,@IsActive
)
EXECUTE sch_AM.usp_GetActiveModel_ID @ModelVersion, @Model_ID OUTPUT
SELECT @ExistsInDM = COUNT(Model_ID)
FROM [sch_AM].[tblDMModelToTable] WITH (NOLOCK)
WHERE Model_ID = @Model_ID
IF @ExistsInDM = 0
BEGIN
INSERT INTO [sch_AM].[tblDMModelToTable]
SELECT
@Model_ID
,[Table_ID]
,[TableVersion]
,[UserViewable]
,[TableRequiredByModel]
FROM [sch_AM].[tblDMModelToTable] WITH (NOLOCK)
WHERE Model_ID = 1
END
END
SELECT * FROM [sch_AM].[tblDMModelVersion] WHERE Model_ID = @Model_ID
SELECT * FROM [sch_AM].[tblDMModelToTable] WHERE Model_ID = @Model_ID
beginner sql t-sql
add a comment |Â
up vote
1
down vote
favorite
This is a simple script which inserts data into a couple of tables. I'm very new to SQL so any feeback, of any kind, would be much appreciated!
DECLARE
@Model_ID INT
, @ModelVersion VARCHAR(10)
, @ExistsInDM INT
, @IsActive BIT
SET @ModelVersion = '2.46.7' -- new version number
SET @IsActive = '1'
SELECT @Model_ID = Model_ID
FROM [sch_AM].[tblDMModelVersion] WITH (NOLOCK)
WHERE [ModelVersion] = @ModelVersion
IF @Model_ID IS NULL
BEGIN
INSERT INTO [sch_AM].[tblDMModelVersion]
( [ModelVersion]
,[IsActive]
)
VALUES ( @ModelVersion
,@IsActive
)
EXECUTE sch_AM.usp_GetActiveModel_ID @ModelVersion, @Model_ID OUTPUT
SELECT @ExistsInDM = COUNT(Model_ID)
FROM [sch_AM].[tblDMModelToTable] WITH (NOLOCK)
WHERE Model_ID = @Model_ID
IF @ExistsInDM = 0
BEGIN
INSERT INTO [sch_AM].[tblDMModelToTable]
SELECT
@Model_ID
,[Table_ID]
,[TableVersion]
,[UserViewable]
,[TableRequiredByModel]
FROM [sch_AM].[tblDMModelToTable] WITH (NOLOCK)
WHERE Model_ID = 1
END
END
SELECT * FROM [sch_AM].[tblDMModelVersion] WHERE Model_ID = @Model_ID
SELECT * FROM [sch_AM].[tblDMModelToTable] WHERE Model_ID = @Model_ID
beginner sql t-sql
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
This is a simple script which inserts data into a couple of tables. I'm very new to SQL so any feeback, of any kind, would be much appreciated!
DECLARE
@Model_ID INT
, @ModelVersion VARCHAR(10)
, @ExistsInDM INT
, @IsActive BIT
SET @ModelVersion = '2.46.7' -- new version number
SET @IsActive = '1'
SELECT @Model_ID = Model_ID
FROM [sch_AM].[tblDMModelVersion] WITH (NOLOCK)
WHERE [ModelVersion] = @ModelVersion
IF @Model_ID IS NULL
BEGIN
INSERT INTO [sch_AM].[tblDMModelVersion]
( [ModelVersion]
,[IsActive]
)
VALUES ( @ModelVersion
,@IsActive
)
EXECUTE sch_AM.usp_GetActiveModel_ID @ModelVersion, @Model_ID OUTPUT
SELECT @ExistsInDM = COUNT(Model_ID)
FROM [sch_AM].[tblDMModelToTable] WITH (NOLOCK)
WHERE Model_ID = @Model_ID
IF @ExistsInDM = 0
BEGIN
INSERT INTO [sch_AM].[tblDMModelToTable]
SELECT
@Model_ID
,[Table_ID]
,[TableVersion]
,[UserViewable]
,[TableRequiredByModel]
FROM [sch_AM].[tblDMModelToTable] WITH (NOLOCK)
WHERE Model_ID = 1
END
END
SELECT * FROM [sch_AM].[tblDMModelVersion] WHERE Model_ID = @Model_ID
SELECT * FROM [sch_AM].[tblDMModelToTable] WHERE Model_ID = @Model_ID
beginner sql t-sql
This is a simple script which inserts data into a couple of tables. I'm very new to SQL so any feeback, of any kind, would be much appreciated!
DECLARE
@Model_ID INT
, @ModelVersion VARCHAR(10)
, @ExistsInDM INT
, @IsActive BIT
SET @ModelVersion = '2.46.7' -- new version number
SET @IsActive = '1'
SELECT @Model_ID = Model_ID
FROM [sch_AM].[tblDMModelVersion] WITH (NOLOCK)
WHERE [ModelVersion] = @ModelVersion
IF @Model_ID IS NULL
BEGIN
INSERT INTO [sch_AM].[tblDMModelVersion]
( [ModelVersion]
,[IsActive]
)
VALUES ( @ModelVersion
,@IsActive
)
EXECUTE sch_AM.usp_GetActiveModel_ID @ModelVersion, @Model_ID OUTPUT
SELECT @ExistsInDM = COUNT(Model_ID)
FROM [sch_AM].[tblDMModelToTable] WITH (NOLOCK)
WHERE Model_ID = @Model_ID
IF @ExistsInDM = 0
BEGIN
INSERT INTO [sch_AM].[tblDMModelToTable]
SELECT
@Model_ID
,[Table_ID]
,[TableVersion]
,[UserViewable]
,[TableRequiredByModel]
FROM [sch_AM].[tblDMModelToTable] WITH (NOLOCK)
WHERE Model_ID = 1
END
END
SELECT * FROM [sch_AM].[tblDMModelVersion] WHERE Model_ID = @Model_ID
SELECT * FROM [sch_AM].[tblDMModelToTable] WHERE Model_ID = @Model_ID
beginner sql t-sql
asked May 24 at 13:46
CallumDA
195210
195210
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
0
down vote
Just 1 will work here. It will do a cast to 1
SET @IsActive = 1
You can also assign in the declare
DECLARE
@Model_ID INT
, @ModelVersion VARCHAR(10) = 'lasdf'
, @ExistsInDM INT
, @IsActive BIT = 1
SELECT @Model_ID = Model_ID
FROM [sch_AM].[tblDMModelVersion] WITH (NOLOCK)
WHERE [ModelVersion] = @ModelVersion
Above will get the last value read.
with (nolock)
Is typically not advised.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Just 1 will work here. It will do a cast to 1
SET @IsActive = 1
You can also assign in the declare
DECLARE
@Model_ID INT
, @ModelVersion VARCHAR(10) = 'lasdf'
, @ExistsInDM INT
, @IsActive BIT = 1
SELECT @Model_ID = Model_ID
FROM [sch_AM].[tblDMModelVersion] WITH (NOLOCK)
WHERE [ModelVersion] = @ModelVersion
Above will get the last value read.
with (nolock)
Is typically not advised.
add a comment |Â
up vote
0
down vote
Just 1 will work here. It will do a cast to 1
SET @IsActive = 1
You can also assign in the declare
DECLARE
@Model_ID INT
, @ModelVersion VARCHAR(10) = 'lasdf'
, @ExistsInDM INT
, @IsActive BIT = 1
SELECT @Model_ID = Model_ID
FROM [sch_AM].[tblDMModelVersion] WITH (NOLOCK)
WHERE [ModelVersion] = @ModelVersion
Above will get the last value read.
with (nolock)
Is typically not advised.
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Just 1 will work here. It will do a cast to 1
SET @IsActive = 1
You can also assign in the declare
DECLARE
@Model_ID INT
, @ModelVersion VARCHAR(10) = 'lasdf'
, @ExistsInDM INT
, @IsActive BIT = 1
SELECT @Model_ID = Model_ID
FROM [sch_AM].[tblDMModelVersion] WITH (NOLOCK)
WHERE [ModelVersion] = @ModelVersion
Above will get the last value read.
with (nolock)
Is typically not advised.
Just 1 will work here. It will do a cast to 1
SET @IsActive = 1
You can also assign in the declare
DECLARE
@Model_ID INT
, @ModelVersion VARCHAR(10) = 'lasdf'
, @ExistsInDM INT
, @IsActive BIT = 1
SELECT @Model_ID = Model_ID
FROM [sch_AM].[tblDMModelVersion] WITH (NOLOCK)
WHERE [ModelVersion] = @ModelVersion
Above will get the last value read.
with (nolock)
Is typically not advised.
answered May 25 at 13:49
paparazzo
4,8131730
4,8131730
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195088%2fsimple-script-to-insert-data%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password