Showing posts with label creating. Show all posts
Showing posts with label creating. Show all posts

Wednesday, March 28, 2012

How do I know if there is a duplicate value in database

hello, i am creating a user login system. When people register for the site I need a way for sql to check weather or not their is a duplicate username already in the databse. Currently, I am going through the whole member profile table searching for a duplicate name. Is there a better way to do this?i am not sure how u r performing the check and what is bothering u. however, u can create a unique index on that field and fire the insert without any check. a duplicate value will produce an error.|||Currently, I am going through the whole member profile table searching for a duplicate name. Is there a better way to do this?
If for some strange reason the unique index (actually this should be the primary key) is not an option for you, why do you "go through the whole table"?.
A simple

SELECT couint(*) from user_profile WHERE username = 'input_value';

will also tell you if there is another row with that username.|||hello, i am creating a user login system. When people register for the site I need a way for sql to check weather or not their is a duplicate username already in the databse. Currently, I am going through the whole member profile table searching for a duplicate name. Is there a better way to do this?

SELECT username, count(*)
FROM tblLogins
GROUP BY username
HAVING COUNT(*) > 1|||INSERT INTO Table(Collist) SELECT values
SELECT @.error = @.@.ERROR
IF @.@.ERROR <> 0
BEGIN
Error Handling|||hmmmm..so which way is the fastest and most efficient?|||I'd say the most efficient would be the unique index (or primary key) option, because, with correct error handling, not only can you prevent the problem from occurring, you can also provide a meaningful error message to users.sql

Friday, March 23, 2012

How do I implement paging please? - DataReader Object

Hi, I am a beginner in ASP.NET 2.0

I am creating a simple blog website.

I am using a DataReader object to collect all of the entries in the database (in one full sweep) and then display them by adding rows to a table and populating them.

I would like to enable paging so that only 5 blog entries at a time are displayed. The user should be able to page backwards and forwards 5 at a time.

Please can somebody give me some ideas of how I should do this?

Should I read all of the data into some kind of list so that all of the data is then readily available? An arraylist or something?

I realise that the DataReader object reads in one continuous stream so I couldn't expect it to read backwards and forwards from the database.

I include a portion of my code below which reads the database.

Many thanks.

Dim objCommand As New SqlCommand
objCommand.Connection = objConnection
objConnection.Open()
objCommand.CommandText = "SELECT BlogID, Date, Blog FROM BlogSite2_Blogs ORDER BY Date DESC"

Dim objReader As SqlDataReader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)

Dim dateDateTime As Date
Dim strBlogEntry As String
Dim intBlogID As Integer

Do While objReader.Read()
intBlogID = objReader.GetValue(0)
dateDateTime = objReader.GetDateTime(1)
strBlogEntry = objReader.GetString(2)

CreateBlogRow(intBlogID, dateDateTime, strBlogEntry)
Loop

objReader.Close()

check this link

http://weblogs.asp.net/scottgu/archive/2006/01/07/434787.aspx

|||

Hi

I would do it in Database with stored procedure.

In the SP described bellow you Just need to pass in a short SQL statement, the Order By clause, and the start row and end row you'd like to return in the resultset.

CREATE PROCEDURE ReturnPage(@.Select varchar(1000), @.OrderByvarchar(1000), @.StartRowint, @.EndRowint)ASBEGIN declare @.ColListvarchar(2000);declare @.Where varchar(2000);declare @.iint;declare @.i2int;declare @.tmpvarchar(1000);declare @.decvarchar(1000);declare @.fvarchar(100);declare @.dvarchar(100);declare @.Symbolchar(2);declare @.SQLvarchar(5000);declare @.Sortvarchar(1000);set @.Sort = @.OrderBy +', 'set @.dec =''set @.Where =''set @.SQL =''set @.i = charindex(',' , @.Sort)while @.i != 0begin set @.tmp =left(@.Sort,@.i-1)set @.i2 = charindex(' ', @.tmp)set @.f =ltrim(rtrim(left(@.tmp,@.i2-1)))set @.d =ltrim(rtrim(substring(@.tmp,@.i2+1,100)))set @.Sort =rtrim(ltrim(substring(@.Sort,@.i+1,100)))set @.i = charindex(',', @.Sort)set @.symbol =casewhen @.d ='ASC'then'>'else'<'end +casewhen @.i=0then'='else''end set @.dec = @.dec +'declare @.' + @.f +' sql_variant; 'set @.ColList =isnull(replace(replace(@.colList,'>','='),'<','=') +' and ','') + @.f + @.Symbol +' @.' + @.fset @.Where = @.Where +' OR (' + @.ColList +') 'set @.SQL = @.SQL +', @.' + @.f +'= ' + @.fend set @.SQL = @.dec +' ' +'SET ROWCOUNT ' +convert(varchar(10), @.StartRow) +'; ' +'SELECT ' +substring(@.SQL,3,7000) +' from (' + @.Select +') a ORDER BY ' + @.OrderBy +'; ' +'SET ROWCOUNT ' +convert(varchar(10), 1 + @.EndRow - @.StartRow) +'; ' +'select * from (' + @.Select +') a WHERE ' +substring(@.Where,4,7000) +' ORDER BY ' + @.OrderBy +'; SET ROWCOUNT 0;'exec(@.SQL)END
Pls refer toEfficient and DYNAMIC Server-Side Paging with T-SQLfor details.
|||

Thank you very much indeed for this Young Fang. I don't understand it all but I shall have a good read and no doubt, I shall learn a lot of new things :)

Wednesday, March 21, 2012

how do I get the uniqueidentifier of just inserted row?

Hello there!

it was a while since i studied SQL and that brings us to my problem...

I'm creating a Stored Procedure wich first insert information in a table. That table has a uniqueidentifier fild that is default-set to newid().

later in the SP i need that uniqueidentifier value? how do I get it?

I tried this:


CREATE PROCEDURE spInsertNews
@.uidArticleId uniqueidentifier = newid,
@.strHeader nvarchar(300),
@.strAbstract nvarchar(600),
@.strText nvarchar(4000),
@.dtDate datetime,
@.dtDateStart datetime,
@.dtDateStop datetime,
@.strAuthor nvarchar(200),
@.strAuthorEmail nvarchar(200),
@.strKeywords nvarchar(400),
@.strCategoryName nvarchar(200) = 'nyhet'
AS
INSERT INTO tblArticles
VALUES( @.uidArticleId,@.strHeader,@.strAbstract,@.strText,@.dt
Date,@.dtDateStart,@.dtDateStop,@.strAuthor,@.strAutho
rEmail,@.strKeywords)

declare @.uidCategoryId uniqueidentifier
EXEC spGetCategoryId @.strCategoryName, @.uidCategoryId OUTPUT

INSERT INTO tblArticleCategory(uidArticleId, uidCategoryId)
VALUES(@.uidArticleId, @.uidCategoryId)

But i get an error when I EXEC the SP like this:


EXEC spInsertNews
@.strHeader = 'Detta är den andra nyheten',
@.strAbstract = 'dn första insatt med sp:n',
@.strText = 'här kommer hela nyhetstexten att stå. Här får det plats 2000 tecken, dvs fler än vad jag orkar skriva nu...',
@.dtDate = '2003-01-01',
@.dtDateStart = '2003-01-01',
@.dtDateStop = '2004-01-01',
@.strAuthor = 'David N',
@.strAuthorEmail = 'david@.davi.com',
@.strKeywords = 'nyhet, blajblaj, blaj'

the errormessage is: Syntax error converting from a character string to uniqueidentifier.

does anyone have a sulution to this problem?
Can I use something similar to the @.@.IDENTITY?
I will be greatful for any ideas...

thanks
/David, SwedenTry this

Declare @.seed int

set @.seed = @.@.Identity

return @.seed

Sam|||Hi,

Though you have default specified in your table as newid() , i would supress the default and generate a newid() in the procedure itself and force that in the Insert statement.

This way, you don't have to go back to the table to find out the last added newid() as you yourself are generating it in you proecure.

Regards,
Navneet|||I posted the same questioned and got back the following answer

or use ScopeIdentity. It returns the auto increment value in the current scope.

@.@.Identity can return a value from other tables.

Scope only returns what its in.|||thanks for the help... I solved it like this:

instead of having the SP recieve a parameter as uniqueidentifier
I created it inside the SP and gave it the value newid...

works fine, thanks|||::I posted the same questioned and got back the following answer
::
::or use ScopeIdentity. It returns the auto increment value in the current scope.
::
::@.@.Identity can return a value from other tables.

You may not have realized this - he is not using an identity field, so none of your solutions are relevant. I doubt it was teh same question, btw. YOu propably were using an identity field.|||The safest way is to use this T-SQL syntax after the INSERT query:

SET @.yourNewId = SCOPE_IDENTITY()|||::The safest way is to use this T-SQL syntax after the INSERT query:
::
::SET @.yourNewId = SCOPE_IDENTITY()

Really?

My documentation says that SCOPE_IDENTITY is for identity fields, not for GUID's.

Now, who is wrong? You or the documentation.|||oops, my mistake, read over the "GUID" part. I was thinking in int identity fields :-)

how do I get the uniqueidentifier of just inserted row?

Hello there!

it was a while since i studied SQL and that brings us to my problem...

I'm creating a Stored Procedure wich first insert information in a table. That table has a uniqueidentifier fild that is default-set to newid().

later in the SP i need that uniqueidentifier value? how do I get it?

I tried this:

CREATE PROCEDURE spInsertNews
@.uidArticleId uniqueidentifier = newid,
@.strHeader nvarchar(300),
@.strAbstract nvarchar(600),
@.strText nvarchar(4000),
@.dtDate datetime,
@.dtDateStart datetime,
@.dtDateStop datetime,
@.strAuthor nvarchar(200),
@.strAuthorEmail nvarchar(200),
@.strKeywords nvarchar(400),
@.strCategoryName nvarchar(200) = 'nyhet'
AS
INSERT INTO tblArticles
VALUES( @.uidArticleId,@.strHeader,@.strAbstract,@.strText,@.dt Date,@.dtDateStart,@.dtDateStop,@.strAuthor,@.strAutho rEmail,@.strKeywords)

declare @.uidCategoryId uniqueidentifier
EXEC spGetCategoryId @.strCategoryName, @.uidCategoryId OUTPUT

INSERT INTO tblArticleCategory(uidArticleId, uidCategoryId)
VALUES(@.uidArticleId, @.uidCategoryId)

But i get an error when I EXEC the SP like this:

EXEC spInsertNews
@.strHeader = 'Detta r den andra nyheten',
@.strAbstract = 'dn frsta insatt med sp:n',
@.strText = 'hr kommer hela nyhetstexten att st. Hr fr det plats 2000 tecken, dvs fler n vad jag orkar skriva nu...',
@.dtDate = '2003-01-01',
@.dtDateStart = '2003-01-01',
@.dtDateStop = '2004-01-01',
@.strAuthor = 'David N',
@.strAuthorEmail = 'david@.davi.com',
@.strKeywords = 'nyhet, blajblaj, blaj'

the errormessage is: Syntax error converting from a character string to uniqueidentifier.

does anyone have a sulution to this problem?
Can I use something similar to the @.@.IDENTITY?
I will be greatful for any ideas...

thanks
/David, SwedenNever mind...

i solved it.

Here's the working code...

CREATE PROCEDURE spInsertNews
@.strHeader nvarchar(300),
@.strAbstract nvarchar(600),
@.strText nvarchar(4000),
@.dtDate datetime,
@.dtDateStart datetime,
@.dtDateStop datetime,
@.strAuthor nvarchar(200),
@.strAuthorEmail nvarchar(200),
@.strKeywords nvarchar(400),
@.strCategoryName nvarchar(200) = 'nyhet'
AS
DECLARE @.uidArticleId uniqueidentifier
SET @.uidArticleId = newid

INSERT INTO tblArticles
VALUES( @.uidArticleId,@.strHeader,@.strAbstract,@.strText,@.dt
Date,@.dtDateStart,@.dtDateStop,@.strAuthor,@.strAutho
rEmail,@.strKeywords)

declare @.uidCategoryId uniqueidentifier
EXEC spGetCategoryId @.strCategoryName, @.uidCategoryId OUTPUT

INSERT INTO tblArticleCategory(uidArticleId, uidCategoryId)
VALUES(@.uidArticleId, @.uidCategoryId)sql