I'm trying to get the last identity from my table 'usergold' and insert it in a new table table with insert value. I used select "IDENT_CURRENT('UserGold') As userid", new value should be in userid.
But I'm getting an error when I put it in insert, I cannot create the trigger. Please check my code.
create trigger addrole
on UserGold
after Insert
as
Begin
select IDENT_CURRENT('UserGold') As userid
insert into AspNetUserRoles values(userid,'2c258e8d-c648-4b38-9b01-989d4dd525fe')
end
i tried
create trigger addrole
on UserGold
after Insert
as
Begin
declare
#userid nvarchar(50)
select #userid=userId from UserGold where userId=IDENT_CURRENT('UserGold')
insert into AspNetUserRoles(UserId,RoleId) values(#userid,'2c258e8d-c648-4b38-9b01-989d4dd525fe')
end
igot this error "Error converting data type nvarchar to numeric " from
select #userid=userId from UserGold where userId=IDENT_CURRENT('UserGold')
Your INSERT statement is not written correctly.
Try
INSERT INTO AspNetUserRoles ( [col_1_name], [col_2_name] )
VALUES (
( SELECT IDENT_CURRENT ( 'UserGold' ) ),
'2c258e8d-c648-4b38-9b01-989d4dd525fe'
);
Note: Replace the column holder names ( col_1_name, col_2_name ) with the actual column names being inserted to.
I have the below data available in a table
DECLARE #AddressTbl As Table (ID int identity,Address varchar(100))
INSERT INTO #AddressTbl
VALUES ('State:AndhraPradesh,Dist:Prakasam')
Next time when I enter the same value I should be notified that this value exists in the table.
For this I will use an sp with warning message that the data is available. But I want how to implement the logic to compare the data.
Create Procedure usp_InsertAddress
(
#Address varchar(100)
)
AS
DECLARE #ID INT
SELECT #ID=(SELECT ID FROM #AddressTbl WHERE Address = #Address)
IF #ID IS NULL
BEGIN
INSERT INTO #AddressTbl
VALUES ('State:AndhraPradesh,Dist:Prakasam')
END;
I may enter the address like 'Dist:Prakasam,State:AndhraPradesh'
and there may be some blank spaces also. So need to parse the address and check the key and values.
I will use permanent table instead of table variable.
Appreciate your help.
You can use if exists for check. Or you can add unique CONSTRAINT in your table. The UNIQUE constraint ensures that all values in a column are different. This will return error if you are trying to insert duplicate value.
Create Procedure usp_InsertAddress
(
#Address varchar(100)
)
AS
if exists(SELECT ID FROM #AddressTbl WHERE Address = #Address)
begin
select 'Value is Already Exist in Table'---For Warning
end
else
BEGIN
INSERT INTO #AddressTbl
VALUES ('State:AndhraPradesh,Dist:Prakasam')
select 'Value Inserted Sucessful'---For Success
END;
Is there a way to select top 1 row from 'User Defined Table Type' which is passed as a parameter to Stored Procedure?
CREATE TYPE t1 as TABLE
(
id INT,
name VARCHAR(100)
)
SP
CREATE STORED PROCEDURE sp1
(
#type dbo.t1 as READONLY
)
BEGIN
SELECT TOP 1 name FROM #type
END
Any Ideas?
Your stored procedure should probably be something like this:
CREATE STORED PROCEDURE sp1
(
#type dbo.t1 READONLY
)
BEGIN
SELECT TOP 1 name
FROM #type t1
ORDER BY id
END
Notes:
I've removed the as keyword
using TOP n without using the ORDER BY clause will return a single row, but there is no guarantee what row it will be, since database tables are un sorted by nature.
Look at below working example, which give detail idea about how to CREATE/PASS/CALL user defined Table Type from SP.
User Defined Table Type Creation Script:
CREATE TYPE [dbo].[IdCenterIdList] AS TABLE(
[Id] [varchar](36) NOT NULL,
[CenterId] [varchar](36) NOT NULL
)
SP Creation:
CREATE PROCEDURE TestType
#IdCenterIdList AS IdCenterIdList ReadOnly
AS
BEGIN
SELECT
TOP(1) *
FROM #IdCenterIdList
END
GO
SP Execute Statement:
DECLARE #tblTestType IdCenterIdList
INSERT INTO #tblTestType VALUES('11','1111')
,('22','222')
,('33','333')
EXEC TestType #tblTestType
Output:
I have a problem with this code :
ALTER PROCEDURE [arch].[spInsertToDocumentFileCategoryRelation]
#DocumentNumber nvarchar(50)
,#DocumentDate date
,#IsDocumentDelete bit
,#Address nvarchar(max)
,#DocumentLevelTypeId int
,#DocumentId2 bigint
,#DocumentRelationTypeId tinyint
,#CategoryId bigint
,#FileId bigint
,#FileAssignCategoryId bigint OUTPUT
,#DocumentRelationId bigint OUTPUT
,#FileOfDocumentId bigint OUTPUT
,#DocumentId bigint OUTPUT
AS
BEGIN
INSERT INTO [arch].[Document]
(
[DocumentNumber]
,[DocumentDate]
,[DateOfDocumentCreate]
,[IsDocumentDelete]
,[Address]
,[DocumentLevelTypeId]
)
VALUES
(
#DocumentNumber
,#DocumentDate
,'2015-5-12'
,#IsDocumentDelete
,#Address
,#DocumentLevelTypeId
)
Set #DocumentId = SCOPE_IDENTITY();
INSERT INTO [arch].[FileOfDocument]
(
[DocumentId]
,[FileId]
)
VALUES
(
#DocumentId
,#FileId
)
Set #FileOFDocumentId = SCOPE_IDENTITY();
INSERT INTO [arch].[DocumentRelation]
(
[DocumentRelationTypeId]
,[DocumentId1]
,[DocumentId2]
)
VALUES
(
#DocumentRelationTypeId
,#DocumentId
,#DocumentId2
)
Set #DocumentRelationId = SCOPE_IDENTITY();
INSERT INTO [arch].[FileAssignCategory]
(
[CategoryId]
,[FileId]
)
VALUES
(
#CategoryId
,#FileId
)
Set #FileAssignCategoryId = SCOPE_IDENTITY();
END
this code Execute correctly but when I want to test this Stored Procedure with some input values, sql server report some errors :
Msg 515, Level 16, State 2, Procedure
spInsertToDocumentFileCategoryRelation, Line 27
Cannot insert the value NULL into column 'DocumentTypeId', table
'ffisherDB.arch.Document'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Msg 515, Level 16, State 2, Procedure
spInsertToDocumentFileCategoryRelation, Line 48
Cannot insert the value NULL into column 'DocumentId', table
'ffisherDB.arch.FileOfDocument'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Msg 515, Level 16, State 2, Procedure
spInsertToDocumentFileCategoryRelation, Line 61
Cannot insert the value NULL into column 'DocumentId1', table
'ffisherDB.arch.DocumentRelation'; column does not allow nulls. INSERT
fails.
The statement has been terminated.
(1 row(s) affected)
(1 row(s) affected)
(1 row(s) affected)
I have four Key that set with SCOPE_IDENTITY(). the first one set exactly right but the others fails and above Errors report.
please help me.
Well, from error text it looks like your arch.Document table has non-nullable column DocumentTypeId.
And you're not filling that column here:
INSERT INTO [arch].[Document]
(
[DocumentNumber]
,[DocumentDate]
,[DateOfDocumentCreate]
,[IsDocumentDelete]
,[Address]
,[DocumentLevelTypeId]
)
So this column has its default value (I guess it is null), thus this insert fails, thus #DocumentId is null and remaining inserts fails too...
As per first error message DocumentTypeId field in Document table is not allowing null. You should insert a value into it in the insert query.
Change your insert to
INSERT INTO [arch].[Document]
(
[DocumentNumber]
,[DocumentDate]
,[DateOfDocumentCreate]
,[IsDocumentDelete]
,[Address]
,[DocumentLevelTypeId]
,[DocumentTypeId] --Add document type id here
)
VALUES
(
#DocumentNumber
,#DocumentDate
,'2015-5-12' --You better insert a DATE type or in ISO formatted 'yyyymmdd' string.
,#IsDocumentDelete
,#Address
,#DocumentLevelTypeId
,#DocumentTypeId --Insert a non - null value
)
Second & third error messages are due to first error.
I'm working on a MSSQL stored procedure.
I receive a table valued parameter (#accountPropsTVP) and a single variable (#accountID) from the C# server.
#accountPropsTVP has 2 columns:
valueTypeID int
value varchar(max)
Note: I'm never sure how many rows will be in this table.
#accountID is an int.
I need to merge everything received into one table, so that it ends up looking like so:
#temporaryTable:
#accountID (always the same for all rows)
valueTypeID
value
Below is what I have tried, but I get an error:
Msg 112, Level 15, State 4, Procedure insertAccountProps, Line 20
Variables are not allowed in the CREATE TABLE statement.
CREATE PROCEDURE insertAccountProps
-- Received parameters
#accountID int,
#accountPropsTVP accountPropsTVP READONLY
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
-- declare new table
DECLARE #accountPropsTemp TABLE
(
accountID int not null DEFAULT (#accountID),
valueTypeID int not null,
value varchar(max) not null
)
-- insert received TVP into new temp table, so that we can manipulate it (tvp's are read only :( )
INSERT INTO #accountPropsTemp
SELECT *
FROM #accountPropsTVP
-- select all from TVP and add it into temp table created above
INSERT INTO dbo.accountsProps
SELECT *
FROM #accountPropsTemp
END
GO
Maybe there's a simpler way of doing this?
Your issue is here:
DECLARE #accountPropsTemp TABLE
(
accountID int not null DEFAULT (#accountID),
valueTypeID int not null,
value varchar(max) not null
)
You're assigning a variable as a default value which as the error message clearly states is not allowed.
The best option for this is to change your syntax to:
DECLARE #accountPropsTemp TABLE
(
accountID int not null,
valueTypeID int not null,
value varchar(max) not null
)
INSERT INTO
#accountPropsTemp
SELECT
#AccountID
,ValueTypeID
,Value
FROM
#accountPropsTVP