I need to insert Tamil language into SQL Server 2005. I have tried using Insert or Update query, it worked fine. While going to stored procedure, I don't know how to pass the parameter.
ALTER PROCEDURE [dbo].[spr_Sam]
#Row_Id int = NULL,
#Description_Ta nvarchar(MAX) = null
AS
BEGIN
update tblTest set
Description_Ta = #Description_Ta
where Row_Id = #Row_Id
END
exec [dbo].[spr_Sam] 2, 'பெண்டிரேம்';
If I execute this it gets inserted as ?????.
exec [dbo].[spr_Sam] 2, N'பெண்டிரேம்';
If I execute this it gets inserted correctly.. but I don't know how to pass that 'N' from my C# Application. I used a text-box to get that Description_Ta parameter.
C# should add the N automatically if you use SqlDbType.NVarChar for SQLParameter
You must be using SqlDbType.VarChar of course
The MSDN doc for SqlDbType states (my bold)
VarChar: A variable-length stream of non-Unicode characters...
...
NVarChar: A variable-length stream of Unicode characters...
Here is the correct update statement:
update tblTest
set Description_Ta = #Description_Ta
where Row_Id = #Row_Id;
You don't need single quotes around a variable.
But, I think the posting is confused. To call the procedure use:
exec [dbo].[spr_Sam] 2, N'பெண்டிரேம்';
To modify it:
ALTER PROCEDURE [dbo].[spr_Sam]
#Row_Id int = NULL,
#Description_Ta nvarchar(MAX) = null
AS
BEGIN
update tblTest
set Description_Ta = #Description_Ta
where Row_Id = #Row_Id;
END;
You shouldn't have arguments when you define the stored procedure.
Related
Is there any possible way to execute a non query without having to assign it to a LINQ to SQL class?
Let's say I have this procedure and I want to INSERT only if the Town_Name (UNIQUE CONSTRAINT) is non existent. Otherwise, the UPDATE is executed. This procedure works well and does the job when executed through SQL Server. However, I can't manage to execute it through C# code.
I am trying to avoid using SQL client, as part of my coursework, my little application has to be capable of selecting, inserting / updating and deleting data using LINQ.
The approach I'm taking always results in 0 rows affected.
// Call Procedure.
db.P_SaveClient("000000001M", "Test", "Dummy", null, "+35699999999");
-- Procedure to be executed.
CREATE PROCEDURE Appointment.SaveClient
(
#id NVARCHAR(10),
#firstName NVARCHAR(35),
#lastName NVARCHAR(35),
#tel NVARCHAR(12),
#mob NVARCHAR(12)
)
AS
BEGIN
DECLARE #clientCount TINYINT
SELECT #clientCount = COUNT(Client_ID)
FROM Appointment.Client
WHERE Client_ID = #id
IF #clientCount = 0
BEGIN
INSERT INTO Appointment.Client
VALUES (
#id
, #firstName
, #lastName
, #tel
, #mob
)
END
ELSE
BEGIN
UPDATE Appointment.Client
SET Client_ID = #id
, First_Name = #firstName
, Last_Name = #lastName
, Telephone = #tel
, Mobile = #mob
END
END
Some tutorials I found:
https://www.youtube.com/watch?v=dlXT-vE46sc
https://www.youtube.com/watch?v=-PAMtSwplu8
You're looking for a Merge statement to execute in SQL, which you could call via the stored proc. This will allow you to insert or update depending on whether it was found. It can even return the ID of the record inserted which can save you another query.
Merge Town t
using ( select #Town_Name as 'Town_Name')
src on (src.Town_Name = t.Town_Name )
when NOT MATCHED then
insert (Town_Name) values (src.Town_Name)
output INSERTED.Town_ID
See here for syntax and more examples:
https://msdn.microsoft.com/en-us/library/bb510625.aspx
I am creating a stored proc in MS SQL Server 2008.
Here in my SP, after an insert statement, an identity value for that row inserted will be
generated. I am using that in another insert Statement.
#SID int
insert into table1(SNAME) values('Sname')
set #SID=SCOPE_IDENTITY()
insert into table2(SID)values(#SID)
here, my doubt is , is it required to add #SID as a parmeter in C# code.
how to declare a variable that do not expect any parameter ? since it is getting the value
from the SP itself..
Please help
Change the sql from
#SID int
to
DECLARE #SID int
Have a look at DECLARE #local_variable (Transact-SQL)
Variables are declared in the body of a batch or procedure with the
DECLARE statement and are assigned values by using either a SET or
SELECT statement. Cursor variables can be declared with this statement
and used with other cursor-related statements. After declaration, all
variables are initialized as NULL, unless a valu is provided as part
of the declaration.
Declaring variable in SQL Server
Declare #SID int --Declaring variable in SQL Server
insert into table1(SNAME) values('Sname')
set #SID=SCOPE_IDENTITY()
insert into table2(SID)values(#SID)
my doubt is , is it required to add #SID in C# code
The question is do you want this identity to be returned as an output parameter to your class?
If YES then you will have to pass it as an output parameter. Like
#SID INT OUTPUT -- as stored procedure parameter
If NO then all the variables that are required locally within the stored procedure will be declared locally. You only need to change
#SID int
To
DECLARE #SID int
CREATE PROCEDURE SPNAME
#PARAM1 INT,
#PARAM2 VARCHAR(10),
.
.
AS
BEGIN
DECLARE #SID AS int
insert into table1(SNAME) values('Sname')
set #SID=SCOPE_IDENTITY()
insert into table2(SID)values(#SID)
END
For Oracle and DB2 declaring variable is,
CREATE OR REPLACE PROCEDURE Proc1()
IS
Declare
myName in varchar;
STMT VARCHAR(4000);
BEGIN
Select fname into myName from student where fname='x'; // is returning unique value
END;
I have the following stored procedure in an SQL Server 2005 database (meant simply to return the database size in MB).
ALTER PROCEDURE [dbo].[dbSize]
AS
BEGIN
SET NOCOUNT ON;
DECLARE #sizeMb int
DECLARE #DB_NAME varchar(100)
SELECT #DB_NAME = DB_NAME()
SELECT #sizeMb = (size*8)/1024 FROM sys.master_files
WHERE DB_NAME(database_id) = #DB_NAME
AND Name = #DB_NAME
RETURN #sizeMb
END
When I run this in SQL Server Management Studio, it works correctly, returning the current DB Size in MB.
I want this to run inside an application, so I added it to a linq to sql datacontext, which generated the following code:
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.dbSize")]
public int dbSize()
{
IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())));
return ((int)(result.ReturnValue));
}
I call it like so:
int dbSize = db.dbSize();
However, it only returns zero, never anything else. No exceptions of any kind are thrown either.
I experimented with selecting a result and using output parameters, but that didn't help either (the output parameter was zero as well). Any suggestions?
I've run into the same problem. As dumb as it sounds, you have to return a single row with single column containing your value from a stored procedure. L2S doesn't do something akin to ExecuteScalar(...).
If you use a UDF, you'll have better luck. This post speaks to the problem nicely.
I have to insert new records in a database every day from a text file ( tab delimited).
I'm trying to make this into a stored procedure with a parameter for the file to read data from.
CREATE PROCEDURE dbo.UpdateTable
#FilePath
BULK INSERT TMP_UPTable
FROM #FilePath
WITH
(
FIRSTROW = 2,
MAXERRORS = 0,
FIELDTERMINATOR = '\t',
ROWTERMINATOR = '\n'
)
RETURN
Then i would call this stored procedure from my code (C#) specifying the file to insert.
This is obviously not working, so how can i do it ?
Just to be clear the problem here is that i can't pass the parameter #FilePath to the FROM clause, or at least i don't know how.
Sorry, I misunderstood.
You need to create the SQL statement dynamically and then execute it:
CREATE procedure dbo.UpdateTable
#FilePath varchar(max)
AS
declare #sql varchar(max)
declare #parameters varchar(100)
set #parameters = 'FIRSTROW = 2, MAXERRORS = 0, FIELDTERMINATOR = ''\\t'', ROWTERMINATOR = ''\\n'' '
SET #SQL = 'BULK INSERT TMP_UPTable FROM ' + #FilePath + #parameters
EXEC (#SQL)
RETURN
Sorry if I am late here, but I would suggest a different approach - open the file in your C# application and convert it to something more SQL-friendly, a DataTable or even XML. In C# you have complete control over how you parse the files. Then write the stored procedure to accept your DataTable or XML. A DataTable is preferable, but cannot be used with Entity Framework.
There is lots of help around of how to do inserts by joining to this sort of input, and SQL Server is optimised for set operations.
I have a stored procedure:
CREATE PROCEDURE [dbo].[BrandDrugDetailsInsert]
#BrandDrugDetailsID uniqueidentifier OUTPUT,
#BrandDrugID uniqueidentifier
AS
BEGIN
INSERT INTO Pharmacy_BrandDrugDetails(BrandDrugID) OUTPUT INSERTED.BrandDrugDetailsID
VALUES (#BrandDrugID)
END
Anytime I try to retieve the value "#BrandDrugDetailsID" using:
param[0] = new SqlParameter("#BrandDrugDetailsID", SqlDbType.UniqueIdentifier);
param[0].Direction = ParameterDirection.Output;
.
.
.
identity = new Guid(param[0].Value.ToString());
I get a null value.
If I try to execute the stored procedure in SQL Server itself, three values are returned:
BrandDrugDetialsID = "471D08BA-382B-4F83-BECC-F96FEF84B5A5"
#BrandDrugDetialsID = NULL
Return Value = 0
I can't figure out what im doing wrong. Please help me.
I believe that when you use the OUTPUT clause in a INSERT statement, the rows come back as a resultset. So instead of using an OUTPUT parameter in the stored procedure, just run YourSqlCommand.ExecuteScalar(), and BrandDrugDetailsID will come out.
You're not doing anything with the INSERTED.BrandDrugDetailsID bit that would actually place it in the #BrandDrugDetailsID variable. You'll either have to OUTPUT into a table variable and then write the value manually to #BrandDrugDetailsID, or simply use ExecuteScalar to access the single value your procedure currently returns.
Are you sure your stored proc is working. How is the value of INSERTED.BrandDrugDetailsID making it into your output parameter #BrandDrugDetailsID?
DECLARE #TableVar Table (#NewBrandDrugDetailsID uniqueidentifier)
INSERT INTO Pharmacy_BrandDrugDetails(BrandDrugID) OUTPUT INSERTED.BrandDrugDetailsID INTO #TableVar
VALUES (#BrandDrugID)
SELECT #BrandDrugDetailsID = #NewBrandDrugDetailsID FROM #TableVar
** Disclaimer, I've not tested this ! **
For those who are using Entityframework 6 or below, all you need to do is this:
using (var db = new AppEntity)
{
var BrandDrugDetailsID = db.BrandDrugDetailsInsert(BrandDrugID).FirstOrDefault();
}