Insert data in SQL Server stored procedure - stored-procedures

I am trying to add data in single table using a SQL Server stored procedure and I am getting an error
procedure or function x has too many argument specified
Also how can I get newly created ID of record as I need to update multiple tables using these IDs
I am using SQL Server 2012
Many thanks
ALTER PROCEDURE [dbo].[CreateNewFunctionsNavigation]
#FunctionName nvarchar(250),
#Hierarchy_Level int
AS
BEGIN
SET NOCOUNT ON;
INSERT [dbo].[Navigation_Functions] ([FunctionName], [Hierarchy_Level])
VALUES(#FunctionName, #Hierarchy_Level)
END
Execution of stored procedure:
DECLARE #return_value int
EXEC #return_value = [dbo].[CreateNewFunctionsNavigation]
#FunctionName = N'DSD',
#Hierarchy_Level = 3
SELECT 'Return Value' = #return_value
GO
My SQL for Function table
USE [MySolution01_DB]
GO
/****** Object: Table [dbo].[Navigation_Functions] Script Date: 06/01/2015 15:58:57 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Navigation_Functions](
[Function_ID] [int] IDENTITY(1,1) NOT NULL,
[FunctionName] [nvarchar](250) NOT NULL,
[Hierarchy_Level] [int] NOT NULL,
CONSTRAINT [PK_Functions] PRIMARY KEY CLUSTERED
(
[Function_ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

It should be
INSERT INTO [dbo].[Navigation_Functions] ([FunctionName], [Hierarchy_Level])
I created a sample table
CREATE TABLE [dbo].[Navigation_Functions](
[NavigationFunctionId] [int] IDENTITY(1,1) NOT NULL,
[FunctionName] [nvarchar](250) NULL,
[Hierarchy_Level] [int] NULL,
CONSTRAINT [PK_Navigation_Functions] PRIMARY KEY CLUSTERED
(
[NavigationFunctionId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

You can get the last ID by: SELECT SCOPE_IDENTITY();

USE [MySolution01_DB]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[CreateNewFunctionsNavigation]
#FunctionName nvarchar(250),
#Hierarchy_Level INT,
#Function_identity INT OUTPUT
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO [dbo].[Navigation_Functions] ([FunctionName],[Hierarchy_Level] )
VALUES(#FunctionName, #Hierarchy_Level);
SET #Function_identity=SCOPE_IDENTITY()
RETURN #Function_identity
RETURN
END

Related

Linq2DB: Bulk copy failing for float type column in MS Sql server

I am trying to perform bulk copy for into a table that has few fields that are of type "float" in MS SQL Server. I noticed that after bulk copy completed, all these "Float" fields had value of "NULL". These fields allowed null value so these NULLs were there.
I traced the call in SQL Profiler and noticed that "INSERT BULK" command did not have these "float" fields.
I simplified my case to just simple object as below.
public class TestIt
{
public int Id{get;set;}
public string Title { get; set; } = $"Title{DateTime.UtcNow}";
public double? Price { get; set; }
}
The table has following schema.
CREATE TABLE [dbo].[TestIt](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Title] [nvarchar](50) NULL,
[Price] [float] NULL,
CONSTRAINT [PK_TestIt] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
When I execute BulkCopy for a list of these object, I can see that "Title" field was copied correctly. Where was "Price" field always have NULL.
Is this some limitation in Linq2Db or I am doing something wrong.
When I use bulk copy with EF, it works fine.

Entity Framework 6 - SQL Server and Oracle (SaveChanges)

I have an application which uses below (Employee) domain model for SQL Server and Oracle data provider. This model works fine for Oracle data provider, however the same gives error for SQL Server. And when I change the field "ID" from decimal to Int it works fine for SQL Server but gives error in Oracle.
On application configuration I am changing the data provider from i.e. from SQL Server to Oracle or vice-versa.
Cannot insert explicit value for identity column in table 'Employee' when IDENTITY_INSERT is set to OFF.*
ORA-01400: cannot insert NULL into ("Emp"."Employee"."ID")
public class Employee
{
[Key, Column(Order = 0)]
public decimal ID { get; set; }
public string NAME { get; set; }
}
if (ID == null || ID == 0)
{
Model.Employee obj = new Model.Employee();
obj.NAME = Name;
if (Convert.ToInt32(ConfigurationSettings.AppSettings["DataProviderType"]) == "Oracle")
obj.ID = GetNextId();
DbContext.Employees.Add(obj);
DbContext.SaveChanges();
}
SQL Server table script
CREATE TABLE [dbo].[Employee](
[ID] [int] IDENTITY(1,1) NOT NULL,
[NAME] [varchar](256) NULL,
CONSTRAINT [Employee_PK] PRIMARY KEY CLUSTERED
(
[ID] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Oracle table script
CREATE TABLE Employee
(
"ID" NUMBER NOT NULL ENABLE,
"NAME" VARCHAR2(256)
);
CREATE UNIQUE INDEX "PK_Employee" ON "Employee" ("ID");
ALTER TABLE "Employee" ADD CONSTRAINT "Employee_PK" PRIMARY KEY ("ID") ENABLE;
How can I handle this in my application i.e. in DOTNET Side?
With suggestion from #IvanStoev, I have change datatype from int to numeric IDENTITY(1,1) NOT NULL. in sql server. And also handled IDENTITY_INSERT error with below code,
modelBuilder.Entity<Employee>().Property(i => i.ID).HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
Hope this will help someone with similar issue.

EF6.1.3 Join columns (like in hibernate)

I'm using Entitiy Framework 6.1.3 and I want to add a relation from N tables to 1 table with more than 1 join columns. The following example will help you to understand my problem:
Table P (protocol table) with these columns:
protocol_id (int, PK)
message (string)
module (string)
reference_id (int)
protocol_id | module | message | reference_id
-----------------------------------
1 | A | created | 1
2 | A | modified | 1
2 | A | created | 2
3 | B | deleted | 1
4 | B | modified | 1
Table A (module A table) with these columns:
mod_id (int, PK)
mod_x (string)
mod_y (...)
...
mod_id | mod_x | ...
--------------------
1 | abc | ...
2 | xyz | ...
Table B (module B table) with these columns:
mod_id (int, PK)
mod_x (string)
mod_y (...)
...
mod_id | mod_x | ...
--------------------
1 | abc | ...
2 | xyz | ...
I would like to have a navigation property in module A to get all procotols for this row - sthg like that:
where module = 'A' and reference_id = mod_id
(same for module B, module C ...)
In Java/Hibernate, I know you can use more than one ElementJoinColumns (with referenceColumnsNames, ...).
How can I handle this with EF6.1?
Thanks, Markus.
Here is the DDL. It is important that you have the proper referential integrity (RI) set up:
USE [Breaz]
GO
/****** Object: Table [dbo].[TableA] Script Date: 9/14/2016 3:25:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableA](
[mod_id] [int] IDENTITY(1,1) NOT NULL,
[mod_x] [varchar](10) NULL,
CONSTRAINT [PK_TableA_] PRIMARY KEY CLUSTERED
(
[mod_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[TableB] Script Date: 9/14/2016 3:25:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableB](
[mod_id] [int] IDENTITY(1,1) NOT NULL,
[mod_x] [varchar](10) NULL,
CONSTRAINT [PK_tableb] PRIMARY KEY CLUSTERED
(
[mod_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: Table [dbo].[TableP] Script Date: 9/14/2016 3:25:43 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[TableP](
[protocol_id] [int] IDENTITY(1,1) NOT NULL,
[message] [varchar](10) NULL,
[module] [varchar](10) NULL,
[reference_id] [int] NULL,
CONSTRAINT [PK_TableP] PRIMARY KEY CLUSTERED
(
[protocol_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[TableP] WITH CHECK ADD CONSTRAINT [FK_TableP_tablea] FOREIGN KEY([reference_id])
REFERENCES [dbo].[TableA] ([mod_id])
GO
ALTER TABLE [dbo].[TableP] CHECK CONSTRAINT [FK_TableP_tablea]
GO
ALTER TABLE [dbo].[TableP] WITH CHECK ADD CONSTRAINT [FK_TableP_tableb] FOREIGN KEY([reference_id])
REFERENCES [dbo].[TableB] ([mod_id])
GO
ALTER TABLE [dbo].[TableP] CHECK CONSTRAINT [FK_TableP_tableb]
GO
Now add the edmx for the three tables, and your linq should look like this (use your dbcontext instead of BreazEntities22 :
var tableA = e.TableAs.
Where(w => w.TablePs.Any(s => s.module == "A")).ToList();
var tableB = e.TableBs.
Where(w => w.TablePs.Any(s => s.module == "A")).ToList();

error line 86 for stored procedure

I am trying to get this stored procedure to work. I have been working for a month straight, was up till 3 last night, and am approaching burnout. I really want to get this to work as we have an ad campaign that we spent hundreds on sending traffic to.
USE [redhotkitties2005db]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[proc_rhkprod_lock_items_for_paypal]
#cfuseridid bigint
,#transaction_guid uniqueidentifier = NULL
AS
BEGIN
SET NOCOUNT ON;
declare #tblcartID bigint
,#cfuserid bigint
,#tblproductsid bigint
,#quantity bigint
,#localInventoryID varchar(100)
,#taxologykey bigint
,#name varchar(1000)
,#shortDescription varchar(8000)
,#productDescription varchar(8000)
,#UorLorUL varchar(2)
,#unitPrice money
,#shippingWeight numeric(6, 2)
,#overSize tinyint
,#smallPic1 nvarchar(100)
,#largePic1 nvarchar(100)
,#smallPic2 nvarchar(100)
,#largePic2 nvarchar(100)
,#smallPic3 nvarchar(100)
,#largePic3 nvarchar(100)
SET #transaction_guid = coalesce(#transaction_guid,newid())
declare c1 cursor forward_only for
select rhkProd_tblCart.tablePK AS tblcartID
,rhkProd_tblProducts.productID as tblproductsid
,rhkProd_tblCart.quantity
,rhkProd_tblProducts.localInventoryID
,rhkProd_tblProducts.name
,rhkProd_tblProducts.taxologyKey
,rhkProd_tblProducts.shortDescription
,rhkProd_tblProducts.productDescription
,rhkProd_tblProducts.UorLorUL
,rhkProd_tblProducts.unitPrice
,rhkProd_tblProducts.shippingWeight
,rhkProd_tblProducts.overSize
,rhkProd_tblProducts.smallPic1
,rhkProd_tblProducts.largePic1
,rhkProd_tblProducts.smallPic2
,rhkProd_tblProducts.largePic2
,rhkProd_tblProducts.smallPic3
,rhkProd_tblProducts.largePic3
from rhkProd_tblCart
INNER JOIN rhkProd_tblProducts
on rhkProd_tblCart.tblProductsFK = rhkProd_tblProducts.productID
where rhkProd_tblCart = #cfuserid
open c1
fetch next
from c1
into #tblcartID
,#cfuserid
,#tblproductsid
,#quantity
,#localinventoryID
,#name
,#taxologykey
,#shortdescription
,#productdescription
,#UorLorUL
,#unitprice
,#shippingweight
,#oversize
,#smallpic1
,#largepic1
,#smallpic2
,#largepic2
,#smallpic3
,#largepic3
begin
while ##fetch_status = 0
insert into rhkprod_tblpaypallock (
,[cfuserid]
,[transaction_guid]
,[timestamp]
,[tblproductsFK]
,[quantity]
,[localInventoryID]
,[name]
,[taxologykey]
,[shortdescription]
,[productdescription]
,[uorlorul]
,[unitprice]
,[shippingweight]
,[oversize]
,[smallpic1]
,[largepic1]
,[smallpic2]
,[largepic2]
,[smallpic3]
,[largepic3]
)
values (
,#cfuserid
,#transaction_guid
,getdate()
,#tblproductsid
,#quantity
,#localinventoryID
,#name
,#taxologykey
,#shortdescription
,#productdescription
,#UorLorUL
,#unitprice
,#shippingweight
,#oversize
,#smallpic1
,#largepic1
,#smallpic2
,#largepic2
,#smallpic3
,#largepic3
)
update rhkprod_tblproducts
set quantity = quantity - #quantity
where productid = #tblproductsid
and UorLorUL in ('U','L')
fetch next
from c1
into #tblcartID
,#cfuserid
,#tblproductsid
,#quantity
,#localinventoryID
,#name
,#taxologykey
,#shortdescription
,#productdescription
,#UorLorUL
,#unitprice
,#shippingweight
,#oversize
,#smallpic1
,#largepic1
,#smallpic2
,#largepic2
,#smallpic3
,#largepic3
close c1
deallocate c1
end
END
Your INSERT statement has a stray comma at the start:
insert into rhkprod_tblpaypallock (
,[cfuserid]
Should be
insert into rhkprod_tblpaypallock (
[cfuserid]
Your VALUES clause also has a stray comma:
values (
,#cfuserid
Should be
values (
#cfuserid

How to create simple membership sql tables manually?

I'm looking for a tool similar to Aspnet_regsql.exe for ASP .NET Membership just for simpleMemberShip Provider. Creating the tables at runtime is too late, cause a client program uses the same tables.
UserProfile
webpages_Membership
webpages_OAuthMembership
webpages_Roles
webpages_UsersInRoles
Any advice would be great
You could just create the script yourself, using SQL Management Studio, after running your application on your developer machine:
Example follows from a similar task I did (note dates, not sure if anything has changed).
Note: you'll need to create the database and do the normal permission setup etc as you would do for any production system.
/****** Object: Table [dbo].[UserProfile] Script Date: 12/06/2012 18:18:37 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[UserProfile](
[UserId] [int] IDENTITY(1,1) NOT NULL,
[UserName] [nvarchar](56) NOT NULL,
PRIMARY KEY CLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[UserName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[webpages_Membership] Script Date: 12/06/2012 18:18:52 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[webpages_Membership](
[UserId] [int] NOT NULL,
[CreateDate] [datetime] NULL,
[ConfirmationToken] [nvarchar](128) NULL,
[IsConfirmed] [bit] NULL,
[LastPasswordFailureDate] [datetime] NULL,
[PasswordFailuresSinceLastSuccess] [int] NOT NULL,
[Password] [nvarchar](128) NOT NULL,
[PasswordChangedDate] [datetime] NULL,
[PasswordSalt] [nvarchar](128) NOT NULL,
[PasswordVerificationToken] [nvarchar](128) NULL,
[PasswordVerificationTokenExpirationDate] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[webpages_Membership] ADD DEFAULT ((0)) FOR [IsConfirmed]
GO
ALTER TABLE [dbo].[webpages_Membership] ADD DEFAULT ((0)) FOR [PasswordFailuresSinceLastSuccess]
GO
/****** Object: Table [dbo].[webpages_OAuthMembership] Script Date: 12/06/2012 18:19:11 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[webpages_OAuthMembership](
[Provider] [nvarchar](30) NOT NULL,
[ProviderUserId] [nvarchar](100) NOT NULL,
[UserId] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[Provider] ASC,
[ProviderUserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[webpages_Roles] Script Date: 12/06/2012 18:19:26 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[webpages_Roles](
[RoleId] [int] IDENTITY(1,1) NOT NULL,
[RoleName] [nvarchar](256) NOT NULL,
PRIMARY KEY CLUSTERED
(
[RoleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
UNIQUE NONCLUSTERED
(
[RoleName] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
/****** Object: Table [dbo].[webpages_UsersInRoles] Script Date: 12/06/2012 18:19:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[webpages_UsersInRoles](
[UserId] [int] NOT NULL,
[RoleId] [int] NOT NULL,
PRIMARY KEY CLUSTERED
(
[UserId] ASC,
[RoleId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[webpages_UsersInRoles] WITH CHECK ADD CONSTRAINT [fk_RoleId] FOREIGN KEY([RoleId])
REFERENCES [dbo].[webpages_Roles] ([RoleId])
GO
ALTER TABLE [dbo].[webpages_UsersInRoles] CHECK CONSTRAINT [fk_RoleId]
GO
ALTER TABLE [dbo].[webpages_UsersInRoles] WITH CHECK ADD CONSTRAINT [fk_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[UserProfile] ([UserId])
GO
ALTER TABLE [dbo].[webpages_UsersInRoles] CHECK CONSTRAINT [fk_UserId]
GO

Resources