将行插入表中,只有一列,主键和标识

时间:2015-05-11 10:07:42

标签: sql-server tsql insert

这是一个表格定义:

CREATE TABLE dbo.TableOnlyPK
(
    Id tinyint PRIMARY KEY IDENTITY (1, 1)
)

现在我需要通过T-SQL语句将行插入到该表中:我尝试了很少的解决方案,但没有人工作。

INSERT INTO dbo.TableOnlyPK () VALUES ()  -- not worked

2 个答案:

答案 0 :(得分:9)

尝试:

INSERT INTO dbo.TableOnlyPK DEFAULT VALUES

您创建了以下表格:

CREATE TABLE dbo.TableOnlyPK
(
    Id tinyint PRIMARY KEY IDENTITY (1, 1)
)

每次开火:INSERT INTO dbo.TableOnlyPK DEFAULT VALUES,您都会在IDENTITY列中插入一行。

所以如果执行:

INSERT INTO dbo.TableOnlyPK DEFAULT VALUES
INSERT INTO dbo.TableOnlyPK DEFAULT VALUES
INSERT INTO dbo.TableOnlyPK DEFAULT VALUES

结果是:

enter image description here

答案 1 :(得分:2)

<abc>
  <!-- Table nested_category -->
    <nested_category>
        <category_id>31</category_id>
        <name>TEST PREP</name>
        <alias>test-prep</alias>
        <lft>5</lft>
        <rgt>8</rgt>
    </nested_category>
    <nested_category>
        <category_id>32</category_id>
        <name>cricket</name>
        <alias>cricket</alias>
        <lft>6</lft>
        <rgt>7</rgt>
    </nested_category>
    <nested_category>
        <category_id>35</category_id>
        <name>FOREST</name>
        <alias>forest2</alias>
        <lft>1</lft>
        <rgt>4</rgt>
    </nested_category>
    <nested_category>
        <category_id>36</category_id>
        <name>animal</name>
        <alias>animal</alias>
        <lft>2</lft>
        <rgt>3</rgt>
    </nested_category>
</abc>