Showing posts with label numeric. Show all posts
Showing posts with label numeric. Show all posts

Thursday, March 22, 2012

Adding value in a query

I am trying to insert a value numeric + 1 in to db table but i get error when i do this

this is the code

Const SQLAsString ="INSERT INTO [PageHits] ([DefaultPage]) VALUES (@.defaultP)"

Dim myCommandAsNew Data.SqlClient.SqlCommand(SQL, myConnection)myCommand.Parameters.AddWithValue("@.DefaultP" +"1", DefaultP.Text.Trim())

myConnection.Open()

myCommand.ExecuteNonQuery()

myConnection.Close()

The Error:

Must declare the variable '@.defaultP'.

Description:An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details:System.Data.SqlClient.SqlException: Must declare the variable '@.defaultP'.

The code you wrote will send a parameter called @.DefaultP1 to the database.

Did you want to increase an existing value in the database? If so, you must use an UPDATE query.sql

Tuesday, March 6, 2012

adding new column

ALTER TABLE BFTITLE ADD COLUMN [RATETYPE] [NUMERIC] (10) NOT NULL ;
syntax is not working so can any one tell the right syntaxalter Table Bftitle Add [ratetype] [numeric] (10) Not Null|||Read BOL (SQL Server Books On Line) carefully. If a table already has rows in it, it isn't possible to add a NOT NULL column unless you also give the column a default value too (if you think about it, this only makes sense). I'm guessing on this, but I'd suggest trying:ALTER TABLE BFTITLE
ADD COLUMN [RATETYPE] [NUMERIC] (10) NOT NULL
DEFAULT 0-PatP|||ALTER TABLE BFTITLE
ADD COLUMN [RATETYPE] [NUMERIC] (10) NOT NULL
DEFAULT 0
-PatP

Close. This worked for me:

ALTER TABLE BFTITLE
ADD [RATETYPE] [NUMERIC] (10) NOT NULL
DEFAULT 0

I just removed "COLUMN" from your statement. Funny, I did not think it would be possible to add a non-nullable column, but you're right it can be done as long as you define a default value.

Following is proof:

create table foo (bar int)
go

insert foo (bar) values (1)
go
insert foo (bar) values (2)
go
insert foo (bar) values (3)
go
insert foo (bar) values (4)
go

ALTER TABLE foo
ADD [RATETYPE] [NUMERIC] (10) NOT NULL
DEFAULT 0

select * from foo

sp_columns foo