|
I suspect the OP is either doing a training excercise or, Ghu forbid, writing something for a friend in need, one does not know retail and the other does not know software, fun times.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Hi all,
I have a stored procedure below:
ALTER PROCEDURE [dbo].[Usp_Add_ProgramType] (@Type varchar(15), @Description varchar(15), @IsValid bit
, @CreatedBy varchar(500), @ModifiedBy varchar(500))
AS
BEGIN
if not exists(select top 1 1 from [dbo].[ProgramTypeLKP] where [Type]=@Type)
begin
INSERT INTO [dbo].[ProgramTypeLKP] ([Type], [Description], IsValid, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy)
values(@Type, @Description, @IsValid, getdate(), @CreatedBy, GETDATE(), @ModifiedBy)
end
--else
--begin
----Want to return some thing here that makes EF funnction to return 0 or -1 or some value so that I can show user a message that its a duplicate record.
--end
END
Even if the Condition fails the EF method is returning value as 1, but I want to get a value on which I will show the end user a message that he is trying to attempt to insert a duplicate record.
Like in the below script, I am checking for the result or errors, I have to get error in the response, or at least in the result some where so that I can show the user a message, I am using Kendo Grid, Create and Update with Popup (just a foot-note):
function onRequestEnd(e)
{
//Check request type
if (e.type == "create")
{
if ((e.response != null) && ((e.response.Errors != null) || (e.response.data["0"].Result <= 1)))
{
//Set some label value in red only when to say attempt to insert duplicate record
}
}
else if (e.type == "update")
{
if ((e.response != null) && ((e.response.Errors != null) || (e.response.data["0"].Result <= 1)))
{
//Set some label value in red only when to say attempt to insert duplicate record
}
}
}
Can anybody please help me in this regards, thanks in advance.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
ProgramType - I would have assumed that the client app already has a list of prexisting records to check against.
Assuming ProgramType has an identity field to generate the ID I would return the Select @@Scope_Identity after the insert or select -1 in the else segment
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I tried it as below but still in the duplicate situation also it was returning me value 1, I didn't really get it why?
ALTER PROCEDURE [dbo].[Usp_Add_LookupRecord] (@LookupTableId int,
@Type varchar(100), @Description varchar(500), @ForeignKeyId int, @CreatedBy varchar(500), @ModifiedBy varchar(500), @IsValid bit<br />
)
AS
BEGIN
declare @LookupTableName varchar(100)=(select top 1 LookupTableName FROM [dbo].[ListOfLookupTables] WHERE PkListOfLookupTablesId=@LookupTableId)
declare @InsertedId int=0, @FKProgramTypeLKPId int = (select top 1 PKProgramTypeLKPId from ProgramTypeLKP where Type='Mental Health')
IF (@LookupTableName='ProgramTypeLKP')
BEGIN
IF NOT EXISTS(SELECT Top 1 1 FROM dbo.ProgramTypeLKP WHERE [Type]=@Type AND ISNULL(IsValid, 1)=1)
BEGIN
INSERT INTO dbo.ProgramTypeLKP ([Type], [Description], CreatedDate, CreatedBy, ModifiedDate, ModifiedBy, IsValid)
VALUES(LEFT(@Type, 15), LEFT(@Description, 500), GETDATE(), LEFT(@CreatedBy, 500), GETDATE(), LEFT(@ModifiedBy, 500), @IsValid)
SET @InsertedId=SCOPE_IDENTITY()
END
ELSE
BEGIN
RETURN 0
END
END
ELSE IF (@LookupTableName='AddressTypeLKP')
BEGIN
IF NOT EXISTS(SELECT Top 1 1 FROM dbo.AddressTypeLKP WHERE [Description]=@Type AND ISNULL(IsValid, 1)=1)
BEGIN
INSERT INTO dbo.AddressTypeLKP ([Description], FKProgramTypeLKPId, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy, IsValid)
VALUES(LEFT(@Description, 20), @FKProgramTypeLKPId, GETDATE(), LEFT(@CreatedBy, 30), GETDATE(), LEFT(@ModifiedBy, 30), @IsValid)
SET @InsertedId=SCOPE_IDENTITY()
END
ELSE
BEGIN
RETURN 0
END
END
RETURN @InsertedId
END
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
That is one nasty design! I would have a seperate procedure for each table!
indian143 wrote: IF NOT EXISTS(SELECT Top 1 1 FROM dbo.ProgramTypeLKP WHERE [Type]=@Type AND ISNULL(IsValid, 1)=1) Test the result of that query - I'll bet it never finds a record.
The following does not seem to make sense outside of an IF statement.
indian143 wrote: declare @LookupTableName varchar(100)=(select top 1 LookupTableName FROM [dbo].[ListOfLookupTables] WHERE PkListOfLookupTablesId=@LookupTableId)
declare @InsertedId int=0, @FKProgramTypeLKPId int = (select top 1 PKProgramTypeLKPId from ProgramTypeLKP where Type='Mental Health')
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yes I can understand that, but there are 25 tables with same Columns Code and Description, one table is selected with Dropdown to add, update or delete, so I have written just one stored procedure instead of writing multiple ones. That's why I used LookupTableId, Because I have listed all those tables into one LookupTable and the LookupTableId comes from that table.
I think it better suited for this scenario, but yes in general its not a good practice.
About Program Type, its always the same Program Type that's why I just took the id and used it.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
How are you calling the stored procedure? The ExecuteSqlCommand method returns the number of rows affected, not the return value of the stored procedure.
There are two ways to get the return value:
var returnValue = new SqlParameter()
{
ParameterName = "@ReturnValue",
SqlDbType = SqlDbType.Int,
Direction = System.Data.ParameterDirection.Output
};
context.Database.ExecuteSqlCommand("exec dbo.Usp_Add_LookupRecord", returnValue, ...other parameters here...);
int result = (int)returnValue.Value; Or:
int result = context.Database.SqlQuery<int>("exec dbo.Usp_Add_LookupRecord", ... parameters here ...).First();
sql server - EF ExecuteStoredCommand with ReturnValue parameter - Stack Overflow[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I am not sure but its calling the SP in the following way:
public virtual int Usp_Add_LookupRecord(Nullable<int> lookupTableId, string type, string description, Nullable<int> foreignKeyId, string createdBy, string modifiedBy, Nullable<bool> isValid)
{
var lookupTableIdParameter = lookupTableId.HasValue ?
new ObjectParameter("LookupTableId", lookupTableId) :
new ObjectParameter("LookupTableId", typeof(int));
var typeParameter = type != null ?
new ObjectParameter("Type", type) :
new ObjectParameter("Type", typeof(string));
var descriptionParameter = description != null ?
new ObjectParameter("Description", description) :
new ObjectParameter("Description", typeof(string));
var foreignKeyIdParameter = foreignKeyId.HasValue ?
new ObjectParameter("ForeignKeyId", foreignKeyId) :
new ObjectParameter("ForeignKeyId", typeof(int));
var createdByParameter = createdBy != null ?
new ObjectParameter("CreatedBy", createdBy) :
new ObjectParameter("CreatedBy", typeof(string));
var modifiedByParameter = modifiedBy != null ?
new ObjectParameter("ModifiedBy", modifiedBy) :
new ObjectParameter("ModifiedBy", typeof(string));
var isValidParameter = isValid.HasValue ?
new ObjectParameter("IsValid", isValid) :
new ObjectParameter("IsValid", typeof(bool));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction("Usp_Add_LookupRecord", lookupTableIdParameter, typeParameter, descriptionParameter, foreignKeyIdParameter, createdByParameter, modifiedByParameter, isValidParameter);
}
And when I changed the stored procedure to return -3 is record exists, it is returning me -1, here is my SP:
ALTER PROCEDURE [dbo].[Usp_Add_LookupRecord] (@LookupTableId int,
@Type varchar(100), @Description varchar(500), @ForeignKeyId int, @CreatedBy varchar(500), @ModifiedBy varchar(500), @IsValid bit<br />
)
AS
BEGIN
declare @LookupTableName varchar(100)=(select top 1 LookupTableName FROM [dbo].[ListOfLookupTables] WHERE PkListOfLookupTablesId=@LookupTableId)
declare @InsertedId int=0, @FKProgramTypeLKPId int = (select top 1 PKProgramTypeLKPId from ProgramTypeLKP where Type='Mental Health')
IF (@LookupTableName='ProgramTypeLKP')
BEGIN
IF NOT EXISTS(SELECT Top 1 1 FROM dbo.ProgramTypeLKP WHERE [Type]=@Type AND ISNULL(IsValid, 1)=1)
BEGIN
INSERT INTO dbo.ProgramTypeLKP ([Type], [Description], CreatedDate, CreatedBy, ModifiedDate, ModifiedBy, IsValid)
VALUES(LEFT(@Type, 15), LEFT(@Description, 500), GETDATE(), LEFT(@CreatedBy, 500), GETDATE(), LEFT(@ModifiedBy, 500), @IsValid)
SET @InsertedId=SCOPE_IDENTITY()
END
ELSE
BEGIN
RETURN -3
END
END
ELSE IF (@LookupTableName='AddressTypeLKP')
BEGIN
IF NOT EXISTS(SELECT Top 1 1 FROM dbo.AddressTypeLKP WHERE [Description]=@Type AND ISNULL(IsValid, 1)=1)
BEGIN
INSERT INTO dbo.AddressTypeLKP ([Description], FKProgramTypeLKPId, CreatedDate, CreatedBy, ModifiedDate, ModifiedBy, IsValid)
VALUES(LEFT(@Description, 20), @FKProgramTypeLKPId, GETDATE(), LEFT(@CreatedBy, 30), GETDATE(), LEFT(@ModifiedBy, 30), @IsValid)
SET @InsertedId=SCOPE_IDENTITY()
END
ELSE
BEGIN
RETURN -3
END
END
RETURN @InsertedId
END
I don't understand why is it returning the -1 instead of -3, is there any way to get the result -3?
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
-- modified 8-Aug-18 13:02pm.
|
|
|
|
|
Try something like this:
public virtual int Usp_Add_LookupRecord(int? lookupTableId, string type, string description, int? foreignKeyId, string createdBy, string modifiedBy, bool? isValid)
{
var returnValue = new SqlParameter
{
ParameterName = "@ReturnValue",
SqlDbType = SqlDbType.Int,
Direction = System.Data.ParameterDirection.Output
};
var lookupTableIdParameter = new SqlParameter("LookupTableId", SqlDbType.Int)
{
Value = (object)lookupTableId ?? DBNull.Value
};
var typeParameter = new SqlParameter("Type", SqlDbType.VarChar, 100)
{
Value = (object)type ?? DBNull.Value
};
var descriptionParameter = new SqlParameter("Description", SqlDbType.VarChar, 500)
{
Value = (object)description ?? DBNull.Value
};
var foreignKeyIdParameter = new SqlParameter("ForeignKeyId", SqlDbType.Int)
{
Value = (object)foreignKeyId ?? DBNull.Value
};
var createdByParameter = new SqlParameter("CreatedBy", SqlDbType.VarChar, 500)
{
Value = (object)createdBy ?? DBNull.Value
};
var modifiedByParameter = new SqlParameter("ModifiedBy", SqlDbType.VarChar, 500)
{
Value = (object)modifiedBy ?? DBNull.Value
};
var isValidParameter = new SqlParameter("IsValid", SqlDbType.Bit)
{
Value = (object)isValid ?? DBNull.Value
};
Database.ExecuteSqlCommand("exec dbo.Usp_Add_LookupRecord",
returnValue,
lookupTableIdParameter,
typeParameter,
descriptionParameter,
foreignKeyIdParameter,
createdByParameter,
modifiedByParameter,
isValidParameter);
return (int)returnValue.Value;
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes I did it thanks a lot Rich
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
Hello,
120 agents dialing in predictive mode and i have 150 channels.problem is that my database is not able to simultaneously update userstate/channels state .due to it my only 35-40 agents are on call and rest waiting for call.then how can i reduce wait time?is there any database maintenance script available which i can use everyday for database maintanance?i dont want to use manually because it may cause other problem.or how can i overcome this problem?
Any help will be apprecited.
I didn't find the right solution from the Internet.
References:
http://www.vicidial.org/VICIDIALforum/viewtopic.php?t=6180
[url=https://blog.advids.co/20-video-examples-from-it-asset-management-software-solution-and-services/]IT asset management software video[/url]
Thank you.
|
|
|
|
|
What is your DBMS?
What is the OS of the computer it is installed on?
|
|
|
|
|
Spam is not a reference.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
But that has to be some sort of record? ... a link to a 10 year old question
|
|
|
|
|
please help me out ! i have accidentally delete my all data from my portable hard drive. how can i recover my data.
|
|
|
|
|
Did you try to google for "recover deleted data" or something similar?
|
|
|
|
|
Hi,
I need to drop a schema in which there are some tables, is there anyway? when I searched, the links are showing dynamic sql to generate drop table text, I want to check before dropping with if exists condition, if I am dropping them, can somebody give me any link or advice or something? I am also researching about it as I am in little urgency I am writing here if I can get some help faster, any help would be greatly helpful - thanks in advance.
Thanks,
Abdul Aleem
"There is already enough hatred in the world lets spread love, compassion and affection."
|
|
|
|
|
I'm almost certain you can't have a table with no schema so dropping a schema with existing tables should not be possible. I have no idea if it is because it seems so obvious that you should not do this, I have never tried it.
You can move table to another schema and then drop the schema, caveat the transfer does not change the views and procedures referencing the dropped schema. Did this recently, thankfully it was a small database.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
I'm not entirely sure but I think you are trying to
a) determine if there are any tables in a schema
b) if there are tables then drop them
c) then drop the schema
I'm guessing that you are currently getting the error Quote: Cannot drop schema 'InsertYourSchemaName' because it is being referenced by object
The following code steps through all the tables (if any) in a named schema (I've included views as well). It will drop the table (or view) then drop the schema at the end
declare @schemaName varchar(255) = 'Test'
declare @thingsToDrop table (TABLE_NAME varchar(255), TABLE_TYPE varchar(255))
insert into @thingsToDrop (TABLE_NAME, TABLE_TYPE)
SELECT TABLE_NAME, TABLE_TYPE
FROM INFORMATION_SCHEMA.TABLES where TABLE_SCHEMA = @schemaName
DECLARE @name varchar(255)
DECLARE @type varchar(30)
IF CURSOR_STATUS('global','myCursor')>=-1 DEALLOCATE tb_cursor
DECLARE tb_cursor CURSOR FOR
SELECT TABLE_NAME, TABLE_TYPE FROM @thingsToDrop
OPEN tb_cursor
FETCH NEXT FROM tb_cursor INTO @name, @type
DECLARE @sql nvarchar(max)
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE @thing varchar(10)
SET @thing = (SELECT CASE WHEN @type = 'BASE TABLE' THEN 'table'
WHEN @type = 'VIEW' THEN 'view'
END)
SET @sql = CONCAT('DROP ',@thing, ' ',@schemaName, '.', @name)
EXEC sp_executesql @sql
FETCH NEXT FROM tb_cursor INTO @name, @type
END
CLOSE tb_cursor
DEALLOCATE tb_cursor
set @sql = 'DROP SCHEMA ' + quotename(@SchemaName)
exec sp_executesql @sql I tested it with these examples
;CREATE SCHEMA Test;
GO
create table Test.test(id int identity(1,1))
create table Test.test1(id int identity(1,1))
GO
;CREATE VIEW Test.ViewTest AS
SELECT * FROM Test.test UNION ALL SELECT * FROM Test.test1
GO
;CREATE SCHEMA Test;
GO
;CREATE VIEW Test.ViewTest AS
INFORMATION_SCHEMA.TABLES
GO
;CREATE SCHEMA Test;
GO
create table Test.test(id int identity(1,1))
create table Test.test1(id int identity(1,1))
GO
;CREATE SCHEMA Test;
GO
|
|
|
|
|
|
I have a piece of legacy software that I have successfully used on modern Windows systems up to and including Windows 10.
I have just installed it on a laptop running Windows 7 (64-bit) and on that machine only, I get the problem described below.
One component is written in VB6 and uses DAO 2.5/3.51 for database access. It all works fine except for SQL statements of the form:
Alter Table xxxx Add Column yyyy Integer.
The data type of integer is merely an example. Irrespective of the data type, all Add Column requests throw an exception "Can't load DLL: MSJTER35.DLL"
I have checked in C:\Windows\System32 and MSJTER35.DLL is definitely installed on the machine.
As I said, it's only Add Column statements that fail. All other database access, Select, Update etc. work fine.
I know this is all old hat, VB6 etc. but I don't have the resources to reengineer in VB.net and, as previously noted, the whole system is working fine on several Windows 10 boxes.
Any suggestions why I am getting this message and how to fix it?
Keith
|
|
|
|
|
|
Problem solved. The key was that it wasn't saying it couldn't find msjter35.dll (the file was clearly there) it was saying it couldn't load it. After some digging, I discovered that msjter35 has a dependency on msjint35.dll and it was that latter file that was missing.
I have no idea how the machine was delivered to me in this state, but copying msjint35.dll to SysWow64 solved the problem.
Thanks for your interest.
Keith
|
|
|
|
|
Hi,
I have below query which giving 8 records.
declare <a href="https://www.codeproject.com/Members/T">@t</a> table (id int)
declare <a href="https://www.codeproject.com/Members/t2">@t2</a> table(id2 int)
insert into <a href="https://www.codeproject.com/Members/T">@t</a> values(1),(1),(1)
insert into <a href="https://www.codeproject.com/Members/t2">@t2</a> values(1),(1),(1)
select t1.id from <a href="https://www.codeproject.com/Members/T">@t</a> t1 inner join <a href="https://www.codeproject.com/Members/t2">@t2</a> t2 on t1.id=t2.id2
I think we should get 9 records.
Please help
Thanks
|
|
|
|
|
That code produces 9 records here. There must be something else going on.
Execute your query in a new SSMS query document. Try adding SET ROWCOUNT 0 at the top, just in case you've picked up some weird settings from somewhere.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|