AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-15084

Chris L's questions

Martin Hope
Chris L
Asked: 2014-11-07 14:24:33 +0800 CST

Powershell 中的脚本表定义

  • 2

我正在编写一个 powershell 来创建一个重复的表(在另一个模式中),以便进行分区切换*。我在添加列和聚集列存储索引时遇到问题。

这是我到目前为止所拥有的($schemaName 是目标模式)。

<snip for parameters getting passed in>

$database = $server.Databases[$dbName]

$table = new-object Microsoft.SqlServer.Management.Smo.Table ($database, $tblName)

$table.Schema = $schemaName

# Next two lines are not working
foreach ($col in $tblname.columns) { $table.columns.Add($col) }
# $table.Columns.CopyTo($tblName.Columns, 0)

## NEED TO ADD CLUSTERED COLUMN STORE INDEX TO TABLE

Write-Output $table.Columns.Count # Getting zero column count
# This isn't working
# $table.Create()

关于如何添加列/聚集列存储索引的任何想法?还是更好的方法?

  • = PartitionManager,在 codeplex 上,不支持 SQL Server 2014(聚集列存储索引),所以我必须滚动我自己的代码。

编辑标题

sql-server partitioning
  • 1 个回答
  • 1501 Views
Martin Hope
Chris L
Asked: 2014-08-29 06:30:17 +0800 CST

ManagePartition 可执行文件的连接问题

  • 0

我目前正在运行 SQL Server 2014,我们有一些已分区的表,我正在尝试在ManagePartition此处使用该应用程序 ( http://sqlpartitionmgmt.codeplex.com/ )

我在命令行中输入:

C:\>ManagePartition.exe /C:CreateStagingFull /d:<Databasename> /s:<schema> 
                        /t:<TableName> /p:5 /A:<NewTablename> /f:C:/scripts.sql 
                        /O:i /U:<My Login> /P:/<My Password>

我收到以下错误:

ConnectionFailureException:无法连接到服务器(本地)。---> System.Data.SqlClient.SqlException:建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。(提供程序:命名管道提供程序,错误:40 - 无法打开与 SQL Server 的连接)

我已经仔细检查了所有拼写(数据库名称、模式、表等)和开关。

我可以做些什么来解决这个问题(我是表分区的新手)?

或者是否有其他策略可以轻松管理数据/将数据从分区表移动到暂存表?

编辑(我错过了 /S: 开关)

我现在收到此错误:

System.NullReferenceException:对象引用未设置到对象的实例。
在 PartitionManagement.PartitionManager.createStgIndex(Index i,TableViewBase parent) 在 PartitionManagement.PartitionManager.createStgNonclusteredIndexes
( )目的。


它确实创建了新表,带有索引、约束(包括分区约束),但没有数据。

sql-server partitioning
  • 2 个回答
  • 293 Views
Martin Hope
Chris L
Asked: 2013-06-21 12:50:18 +0800 CST

并行运行函数

  • 4

这适用于 SQL Server 2012。

我们有一些 FTP 文件的导入过程,这些文件被拾取并读入暂存表,我们从那里按摩/检查数据,然后再投入生产。导致某些问题的领域之一是日期,有些是有效的,有些是拼写错误,有些只是胡言乱语。

我有以下示例表:

Create Table RawData
(
 InsertID int not null,
 MangledDateTime1 varchar(10) null,
 MangledDateTime2 varchar(10) null,
 MangledDateTime3 varchar(10) null
)

我也有一个目标表(比如在生产中)

Create Table FinalData
(
  PrimaryKeyID int not null, -- PK constraint here, ident
  ForeighKeyID int not null, -- points to InsertID of RawData
  ValidDateTime1 SmallDateTime null,
  ValidDateTime2 SmallDateTime null,
  ValidDateTime3 SmallDateTime null
)

我将以下内容插入到 RawData 表中:

 Insert Into RawData(InsertID, MangledDateTime1, MangledDateTime2, MangledDateTime3)
 Values(1, '20001010', '20800630', '00000000') -- First is legit, second two are not
 Insert Into RawData(InsertID, MangledDateTime1, MangledDateTime2, MangledDateTime3)
 Values(1, '20800630', '20130630', '20000000') -- middle is legit, first/third are not
 Insert Into RawData(InsertID, MangledDateTime1, MangledDateTime2, MangledDateTime3)
 Values(1, '00001010', '00800630', '20130630') -- Last is legit, first two are not

我写了一个函数dbo.CreateDate来解决这个问题。我们尝试尽可能地清理数据(NULL如果我们不能使用),然后将数据转换为正确的数据类型(在这种情况下smalldatetime)。

Insert Into FinalData(ForeighKeyID , ValidDateTime1, ValidDateTime2, ValidDateTime3)
Select 
 InsertID
 ,dbo.CreateDate(MangledDateTime1)
 ,dbo.CreateDate(MangledDateTime2)
 ,dbo.CreateDate(MangledDateTime3)
From RawData

我们遇到了一些函数的性能问题。我想知道它们是否/如何并行工作。

我在这里假设该函数CreateDate在每一行插入时并行运行。这样每个列/值都有它的“自己的”功能,并且在它插入的同时运行。

但我可能错了,它是否在插入时在每一行的每一列上连续运行?

创建日期()代码:

Alter Function dbo.CreateDate
(
@UnformattedString  varchar(12)
)
Returns smalldatetime
As
Begin
Declare @FormattedDate smalldatetime

If(@UnformattedString Is Not Null)
Begin
    Declare @MaxSmallDate varchar(8) = '20790606'


    -- We got gibberish
    If Len(@UnformattedString) = 1
    Begin
        return null
    End

    -- To account for date and time
    If Len(@UnformattedString) = 12
    Begin
        Select @UnformattedString = Substring(@UnformattedString, 0,9)
    End

    If @UnformattedString = '20000000'
    Begin
        Select @UnformattedSTring = @MaxSmallDate
    End

    -- Some people are sending us two digit years, won't parse right
    If Substring(@UnformattedString,0,3) = '00'
    Begin
        Select @UnformattedString = Replace(@UnformattedString, '00','20')
    End

    -- Some people are fat fingering in people born in 18??, so change to 19??
    If Substring(@UnformattedString,0,3) in ('18')
    Begin
        -- We only want to change the year '18', not day 18 
        SELECT @UnformattedString = STUFF(@UnformattedString, 
                           CHARINDEX('18', @UnformattedString), 2, '19')
    End

    -- We're getting gibberish
    If Substring(@UnformattedString,0,3) not in ('19','20') 
               And Len(@UnformattedString) != 6
    Begin
        Select @UnformattedString = Replace(@UnformattedString, 
                       Substring(@UnformattedString,0,3),'20')
    End

    -- If the 4 digit year is greater than current year, set to max date
    If Convert(int, Substring(@UnformattedString,0,5)) > Year(getdate())
    Begin
        Set @FormattedDate = CONVERT(smalldatetime,@MaxSmallDate,1)
    End
    -- If the 4 digit year is less than 100 years ago, set to max date
    Else If Year(getdate()) - Convert(int, Substring(@UnformattedString,0,5)) >= 100
    Begin
        Set @FormattedDate = CONVERT(smalldatetime,@MaxSmallDate,1)
    End
    Else -- valid date(we hope)
    Begin
        Set @FormattedDate = CONVERT(smalldatetime,@UnformattedString,1) 
    End

    
    
End

Return @FormattedDate
End
Go
sql-server performance
  • 1 个回答
  • 2876 Views
Martin Hope
Chris L
Asked: 2013-03-07 07:38:14 +0800 CST

写varchar和nvarchar的区别

  • 65

目前在我们的 SQL Server 2012 数据库中,我们正在使用varchar,我们想更改它nvarchar。我已经生成了一个脚本来做到这一点。

我的问题是 SQL Server 写入varchar列与nvarchar列的方式有什么不同吗?我们有许多我担心的后端程序。

编辑:
不确定这是否有帮助,但列上没有索引、f/k 或约束。

sql-server varchar
  • 5 个回答
  • 252000 Views
Martin Hope
Chris L
Asked: 2013-01-18 11:48:25 +0800 CST

如何创建对象然后更改它

  • 2

我正在使用 sql server 2012:

我想用脚本创建一个对象(如果它不存在),然后改变它。

我试图避免通常的删除/创建,因为它会影响统计数据。它还与我们如何编写对数据库的更改脚本有关。

我有的:

Go
If object_id(N'My_Function', N'FN') IS NULL 
Begin
Create Function My_Function
(
    @SomeParameter int
)
Returns int
As
Begin
    -- Intentionally empty
End

 End

Go
-- A header/comments
Alter Function My_Function
(
@SomeParameter int
)
Returns int
As
Begin
Declare @returnValue int
    -- A bunch of sql code
Return @returnValue
End
Go

我遇到的问题是“Create”必须在脚本中首先出现(或在“GO”之后首先出现),但我不能将“GO”放在“If”条件中。有没有办法有条件地创建不存在的东西,然后再改变它?

sql-server sql-server-2012
  • 1 个回答
  • 113 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目

    • 12 个回答
  • Marko Smith

    如何让sqlplus的输出出现在一行中?

    • 3 个回答
  • Marko Smith

    选择具有最大日期或最晚日期的日期

    • 3 个回答
  • Marko Smith

    如何列出 PostgreSQL 中的所有模式?

    • 4 个回答
  • Marko Smith

    列出指定表的所有列

    • 5 个回答
  • Marko Smith

    如何在不修改我自己的 tnsnames.ora 的情况下使用 sqlplus 连接到位于另一台主机上的 Oracle 数据库

    • 4 个回答
  • Marko Smith

    你如何mysqldump特定的表?

    • 4 个回答
  • Marko Smith

    使用 psql 列出数据库权限

    • 10 个回答
  • Marko Smith

    如何从 PostgreSQL 中的选择查询中将值插入表中?

    • 4 个回答
  • Marko Smith

    如何使用 psql 列出所有数据库和表?

    • 7 个回答
  • Martin Hope
    Jin 连接到 PostgreSQL 服务器:致命:主机没有 pg_hba.conf 条目 2014-12-02 02:54:58 +0800 CST
  • Martin Hope
    Stéphane 如何列出 PostgreSQL 中的所有模式? 2013-04-16 11:19:16 +0800 CST
  • Martin Hope
    Mike Walsh 为什么事务日志不断增长或空间不足? 2012-12-05 18:11:22 +0800 CST
  • Martin Hope
    Stephane Rolland 列出指定表的所有列 2012-08-14 04:44:44 +0800 CST
  • Martin Hope
    haxney MySQL 能否合理地对数十亿行执行查询? 2012-07-03 11:36:13 +0800 CST
  • Martin Hope
    qazwsx 如何监控大型 .sql 文件的导入进度? 2012-05-03 08:54:41 +0800 CST
  • Martin Hope
    markdorison 你如何mysqldump特定的表? 2011-12-17 12:39:37 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 对 SQL 查询进行计时? 2011-06-04 02:22:54 +0800 CST
  • Martin Hope
    Jonas 如何从 PostgreSQL 中的选择查询中将值插入表中? 2011-05-28 00:33:05 +0800 CST
  • Martin Hope
    Jonas 如何使用 psql 列出所有数据库和表? 2011-02-18 00:45:49 +0800 CST

热门标签

sql-server mysql postgresql sql-server-2014 sql-server-2016 oracle sql-server-2008 database-design query-performance sql-server-2017

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve