Insert Statement

烂漫一生 提交于 2019-12-11 03:47:36

问题


I want to insert the values ('testu', 'testp', 'testname', 'testsur', 'testemail') into [dbo].[users]

I'm using Microsoft SQL Server 2012 and anytime i try to query something like

INSERT INTO [dbo].[users]
VALUES ('testu', 'testp', 'testname', 'testsur', 'testemail')

I get an Error on the "INSERT" statement saying: This statement is not recognized in the context. What does this mean? How can i fix it?

CREATE TABLE [dbo].[users] (
    [username] VARCHAR (40) NOT NULL,
    [password] VARCHAR (40) NOT NULL,
    [name]     VARCHAR (40)  NOT NULL,
    [surname]  VARCHAR (40) NOT NULL,
    [email]    VARCHAR (40) NOT NULL,
PRIMARY KEY CLUSTERED ([username] ASC)
);

回答1:


I am running your statement in a database by the name [TestDB]

USE [TestDB]
GO

CREATE TABLE [dbo].[users] 
(
    [username] VARCHAR (40) NOT NULL,
    [password] VARCHAR (40) NOT NULL,
    [name]     VARCHAR (40)  NOT NULL,
    [surname]  VARCHAR (40) NOT NULL,
    [email]    VARCHAR (40) NOT NULL,
PRIMARY KEY CLUSTERED ([username] ASC)
);

GO

INSERT INTO [dbo].[users] VALUES ('testu', 'testp', 'testname', 'testsur', 'testemail')

The result after running the above statement

Next I will try to insert another row to the table as under

INSERT INTO [dbo].[users] VALUES ('testuwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww', 'testp', 'testname', 'testsur', 'testemail')

Result is

Which is an error that indicates that the [UserName] column value length is more than the length specified while the DDL was created.

Solution

A) Either limit the [UserName] length within 40 characters (in this example).

B) Or Increase the length of the [UserName] column by using the ALTER statement.

e.g.

ALTER TABLE [dbo].[users]
ALTER COLUMN [username] VARCHAR (100) NOT NULL

N.B.~ here [username] column has just taken as a reference to point the error. This situation can happen for other columns also. So just verify for which column it is happening.



来源:https://stackoverflow.com/questions/12829405/insert-statement

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!