I am using SQL Server 2008.
This is my Table definition.
create table myTable (nid int identity(1,1),content nvarchar(max));
when I execute these two queries
insert into myTable(content)values(N'हिंदी में परीक्षण');
update myTable set content=N'हिंदी में परीक्षण' where nid=1;
the other language data is inserted/updated in the table. I want to do the same in a stored procedure. This is the definition of my stored procedure.
create proc myProc
#nid int,
#content nvarchar(max)
as
begin
update myTable set content=#content where nid=#nid;
end
it just doesn't work the column is updated with ????????????? value. What should I do. I tried to do this
update myTable set content=N''+#content where nid=#nid;
But this doesn't work as well. Please Help.
I forgot to tell one thing
exec myProc 1,N'हिंदी में परीक्षण'
this does work of course it does the problem is i send data from an asp .net web application where i use this syntax.
public void myProcCall()
{
dbcommand = db.GetStoredProcCommand("myProc", nid, content);
ds = db.ExecuteDataSet(dbcommand);
}
So when I send other language content from the web application the value is updated as ??????????. Do I have to make some changes in my asp .net code.
This will work
EXEC myProc 1, N'हिं में पदी रीक्षण'
This won't
EXEC myProc 1, 'हिं में पदी रीक्षण'
Ergo, the parameter is sent as non-unicode
You need to ensure that the code that populate the parameter sends unicode
Related
I wanted to 'Call' MariaDB Procedure from Azure Data Factory.
How can this be achieved, are there any other service which can be integrated with ADF to call this MariaDB procedures
I tried calling the procedure by writing the query using lookup activity.
It fails while showing this error.
ErrorCode=InvalidParameter,'Type=Microsoft.DataTransfer.Common.Shared.HybridDeliveryException,Message=The value of the property 'columns' is invalid: 'Value cannot be null.
Parameter name: columns'.,Source=,''Type=System.ArgumentNullException,Message=Value cannot be null.
Parameter name: columns,Source=Microsoft.DataTransfer.Common,'
Lookup activity reads and returns the content of the query. I tried to repro this by creating three stored procedures in Azure SQL database for Maria DB.
First Stored procedure is written to update the data in the table.
DELIMITER $$
CREATE PROCEDURE update_inventory()
BEGIN
UPDATE inventory SET quantity = 150
WHERE id = 1;
END$$
DELIMITER ;
When this procedure is called in ADF lookup activity, error occurs.
Second stored procedure is written with select query.
DELIMITER $$
CREATE PROCEDURE select_inventory()
BEGIN
select * from inventory;
END$$
DELIMITER ;
When this SP is called, ADF pipeline is executed successfully.
In order to execute the stored procedure with update statements (or any statements), a select statement is added in the Stored procedure.
DELIMITER $$
CREATE PROCEDURE update_select_inventory()
BEGIN
UPDATE inventory SET quantity = 150
WHERE id = 1;
select * from inventory;
END$$
DELIMITER ;
When this stored procedure is called through Lookup activity, it got executed successfully.
Try adding select statement in the stored procedure and execute it in Lookup activity. Or add Select statement after Call stored procedure statement.
By selecting the 'query' option, you can call the stored procedure using lookup activity. From your error message, it looks like you are missing the parameter columns while calling the stored procedure.
Did you try executing the same code using the client tools like MySQL workbench? If you can execute the stored proc from other client tools, then you should be able to execute the same using the lookup activity.
I tested from my end and was able to execute the Stored procedure using lookup activity. Please see the below screenshot for your reference.
I am trying to create tables dynamically in a SQL Server database.
Like this with input from a textbox:
ALTER PROCEDURE [dbo].[opretNyEsyn]
#Navn NVARCHAR(100)
AS
BEGIN
DECLARE #SQLString NVARCHAR(MAX)
SET #SQLString = 'create table ' + QUOTENAME(#Navn) +
'([EsynNummer][int]Identity(1,1),
[Dato][datetime])'
END
But nothing happens when I run the method from the form, the table isn't created, and I get no errors.
What am I missing?
Notice. This is just a test table. It isn't supposed to look like this in the end.
Thanks in advance
You are just creating the statement, but in order to create the tables, you need to Execute the statement using the EXEC statement of sp_executesql SP. Add an execute statement to your Procedure. Like this
ALTER PROCEDURE [dbo].[opretNyEsyn]
#Navn nvarchar(100)
as
Begin
declare #SQLString nvarchar(max)
set #SQLString = 'create table ' + QUOTENAME(#Navn)+
'([EsynNummer][int]Identity(1,1),
[Dato][datetime])'
exec(#SQLString)-- Execute the Statement
End
Go
You need to execute dynamic SQL:
ALTER PROCEDURE [dbo].[opretNyEsyn]
#Navn SYSNAME
as
Begin
declare #SQLString nvarchar(max);
set #SQLString = 'create table ' + QUOTENAME(#Navn)+
'([EsynNummer][int]Identity(1,1),
[Dato][datetime])';
EXECUTE (#SQLString);
End;
Anyway I recommend to read The Curse and Blessings of Dynamic SQL and Packaging Permissions in Stored Procedures:
CREATE TABLE #tbl
The desire here is to create a table of which the name is determined at run-time.
If we just look at the arguments against using dynamic SQL in stored procedures, few of them are really applicable here. If a stored procedure has a static CREATE TABLE in it, the user who runs the procedure must have permissions to create tables, so dynamic SQL will not change anything. Plan caching obviously has nothing to do with it. Etc.
Nevertheless: Why? Why would you want to do this? If you are creating tables on the fly in your application, you have missed some fundamentals about database design. In a relational database, the set of tables and columns are supposed to be constant. They may change with the installation of new versions, but not during run-time.
i want to filter the rows using Stored procedure. below is my query
Create PROCEDURE [dbo].[Test]
#SearchTerm VARCHAR(100)
AS
BEGIN
Select * from master where name in (#SearchTerm)
END
In code behind,
cmd.Parameters.AddWithValue("#SearchTerm", "peter")
when i run with above parameter, it's work fine.
but when i pass like this
cmd.Parameters.AddWithValue("#SearchTerm", "'peter','rahul'")
this time no rows fetching.
i tried manually then also it's not working.
exec Test ''peter','rahul''
Please help me, how to pass muliple string Using IN clause?
One method is
Create PROCEDURE [dbo].[Test]
#SearchTerm VARCHAR(100)
AS
BEGIN
Select * from master where ','+#SearchTerm+',' like '%,'+name+',%'
You can find more methods at http://www.sommarskog.se/arrays-in-sql-2008.html
I have a web form that inserts a row into an SQL Server 2008 table. Table has a field which must be unique, but for some reasons we are not allowed to use autoincrement utility. Instead of this I select the maximum of this field and increment it from code and insert the row with that id.
The problem is, if more than one person uses this form simultaneously, there occurs a concurrency problem and same id will be inserted to the table. I do not know how to solve this issue in a asp.net web project because all users have their own session and threads.
Any idea on how to manage concurrency problems in asp.net project will be very useful.
Thanks.
If you perform the insert into a stored procedure, or in a transaction in the code, I don't see a reason to have problems with concurrency. Something like:
BEGIN TRAN
DECLARE #MAX int
SELECT #MAX = MAX(ID) FROM MyTable
INSERT INTO MyTable (#MAX, VAlue, Value2..)
COMMIT TRAN
or
SqlCommand command = connection.CreateCommand();
SqlTransaction transaction;
transaction = connection.BeginTransaction("InsertRow");
... perform insert using command object ...
transaction.Commit();
I have an audit record table that I am writing to. I am connecting to MyDb, which has a stored procedure called 'CreateAudit', which is a passthrough stored procedure to another database on the same machine called MyOther DB with a stored procedure called 'CreatedAudit' as well.
In other words in MyDB I have CreateAudit, which does the following EXEC dbo.MyOtherDB.CreateAudit.
I call the MyDb CreateAudit stored procedure from my application, using subsonic as the DAL. The first time I call it, I call it with the following (pseudocode):
int openStatus, closeStatus = 0;
openStatus = Convert.ToInt32(SPs.LogAccess(userId, "OPENED"));
closeStatus = Convert.ToInt32(SPs.LogAccess(userId, "CLOSED"));
This is simplified, but this is what LogAccess calls:
ALTER procedure [dbo].[LogAccess]
#UserID uniqueid,
#Action varchar(10),
#Status integer output
as
DECLARE #mStatus INT
EXEC [MyOtherDb].[dbo].[LogAccess]
#UserID = #UserID,
#Action = #Action,
#Status = #mStatus OUTPUT
select #mStatus
In my second stored procedure it is supposed to mark the record that was created by the CreateAudit(recordId, "Opened") with a status of closed.
This works great if I run them independently of one another, or even if I paste them into query analyzer. However when they execute from the application, the record is not marked as "Closed".
When I run SQL profiler I see that both queries ran, and if I copy the queries out and run them from query analyzer the record gets marked as closed 100% of the time!
When I run it from the application, about once every 20 times or so, the record is successfully marked closed - the other 19 times nothing happens, but I do not get an error!
Is it possible for the .NET app to skip over the ouput from the first stored procedure and start executing the second stored procedure before the record in the first is created?
When I add a "WAITFOR DELAY '00:00:00:003'" to the top of my stored procedure, the record is also closed 100% of the time.
My head is spinning, any ideas why this is happening!
Thanks for any responses, very interested in hearing how this can happen.
In your 1st stored proc, try having the EXEC statement wait for a return value from the 2nd stored proc. My suspicion is that your first SP is firing off the 2nd stored proc and then immediately returning control to your .NET code, which is leading to the above commenter's concurrency issue. (That is to say, the 2nd SP hasn't finished running yet by the time your next DB call is made!)
SP1: EXEC #retval = SP2 ....