给出一张表:
|Name | Hobbies |
-----------------------------------
|Joe | Eating,Running,Golf |
|Dafydd | Swimming,Coding,Gaming |
Run Code Online (Sandbox Code Playgroud)
我想将这些行拆分出来得到:
|Name | Hobby |
----------------------
|Joe | Eating |
|Joe | Running |
|Joe | Golf |
|Dafydd | Swimming |
|Dafydd | Coding |
|Dafydd | Gaming |
Run Code Online (Sandbox Code Playgroud)
我在下面完成了这个(示例已准备好在SSMS中运行),购买我的解决方案使用我觉得很难看的游标.有没有更好的方法呢?我在SQL Server 2008 R2上,如果有任何新的东西可以帮助我.
谢谢
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Split]') and xtype in (N'FN', N'IF', N'TF')) drop function [dbo].Split
go
CREATE FUNCTION dbo.Split (@sep char(1), @s varchar(512))
RETURNS table
AS
RETURN …Run Code Online (Sandbox Code Playgroud) 我在ASP.NET MVC项目中使用Castle Windsor 2.5.1并使用属性注入来创建一个我期望在基本控制器类上始终可用的对象.我正在使用工厂来创建这个对象,但是如果构造函数中有错误,我根本不会收到来自Windsor的警告,它只是返回我的对象但没有注入属性.
这是预期的行为,如果是这样,当工厂无法返回任何内容时,如何引发错误?
这是一个例子
public class MyDependency : IMyDependency
{
public MyDependency(bool error)
{
if (error) throw new Exception("I error on creation");
}
}
public interface IMyDependency
{
}
public class MyConsumer
{
public IMyDependency MyDependency { get; set; }
}
[TestFixture]
public class ProgramTest
{
[Test]
public void CreateWithoutError() //Works as expected
{
var container = new WindsorContainer().Register(
Component.For<IMyDependency>().UsingFactoryMethod(() => new MyDependency(false)).LifeStyle.Transient,
Component.For<MyConsumer>().LifeStyle.Transient
);
var consumer = container.Resolve<MyConsumer>();
Assert.IsNotNull(consumer);
Assert.IsNotNull(consumer.MyDependency);
}
[Test]
public void CreateWithError_WhatShouldHappen() //I would …Run Code Online (Sandbox Code Playgroud) 我试图找到Command在Ruby中调用的某种类型的所有子类,并且我遇到了以下代码,它完美地完成了这个技巧,但是我并不真正理解它是如何工作的,主要是class << [Subtype]部分.我已经尝试过阅读,但我觉得还有一些我不知道的Ruby魔法.有人可以向我解释一下:-)
ObjectSpace.enum_for(:each_object, class << Command; self; end).to_a()
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用一些数据设置Solr索引,但是我想将我的一个字段作为管道分隔发送并将其拆分为Solr端,例如
<doc>
<add>
<field name="cat">a|b|c<field>
</add>
</doc>
Run Code Online (Sandbox Code Playgroud)
对于声明为的多值字段
<field name="cat" type="str_split_on_pipe" indexed="true" stored="true" multiValued="true" omitNorms="true" />
Run Code Online (Sandbox Code Playgroud)
管道类型的拆分是
<fieldType name="str_split_on_pipe" class="solr.TextField" positionIncrementGap="100" >
<analyzer type="index">
<tokenizer class="solr.PatternTokenizerFactory" pattern="\|\s*" />
<filter class="solr.LowerCaseFilterFactory"/>
<!--<filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" catenateAll="0" splitOnCaseChange="0"/>-->
<!-- this filter can remove any duplicate tokens that appear at the same position - sometimes
possible with WordDelimiterFilter in conjuncton with stemming. -->
<filter class="solr.RemoveDuplicatesTokenFilterFactory"/>
</analyzer>
<analyzer type="query">
<tokenizer class="solr.PatternTokenizerFactory" pattern="\|\s*" />
<filter class="solr.LowerCaseFilterFactory"/>
<!--<filter class="solr.WordDelimiterFilterFactory" generateWordParts="1" generateNumberParts="1" catenateWords="0" catenateNumbers="0" catenateAll="0" …Run Code Online (Sandbox Code Playgroud) 我使用存储过程和xp_cmdshell调用远程调用SSIS包:
declare @cmd varchar(5000)
set @cmd = '"C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\dtexec.exe" /Rep E /Sql Package /SET \Package.Variables[User::ImportFileName].Value;c:\foo.xlsx'
print @cmd
exec xp_cmdshell @cmd
Run Code Online (Sandbox Code Playgroud)
这工作得很好,但是我不能保证变量值(C:\ foo.xslx)不会包含空格,所以我想逃避,与像下面的报价:
set @cmd = '"C:\Program Files (x86)\Microsoft SQL Server\100\DTS\Binn\dtexec.exe" /Rep E /Sql Package /SET \Package.Variables[User::ImportFileName].Value;"c:\foo.xlsx"'
Run Code Online (Sandbox Code Playgroud)
但通过这样做,我得到了错误
'C:\Program' is not recognized as an internal or external command, operable program or batch file.
Run Code Online (Sandbox Code Playgroud)
如果CMD.EXE内执行上述两个命令的正常工作,所以我猜测SQL Server在解释我的双引号和改变的东西,但我想不出什么.
鉴于使用以下视图模型和操作DefaultModelBinder,它似乎忽略了字典,但正确绑定了所有其他属性.我在这里错过了什么吗?看一下MVC源代码,这似乎是合法的.
谢谢
public class SomeViewModel
{
public SomeViewModel()
{
SomeDictionary = new Dictionary<string, object>();
}
public string SomeString { get; set; }
public IDictionary<string, object> SomeDictionary { get; set; }
}
[HttpPost]
public ActionResult MyAction(SomeViewModel someViewModel)
{
//someViewModel.SomeString binds correctly
//someViewModel.SomeDictionary is null
}
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage<SomeViewModel>" MasterPageFile="~/Views/Shared/Site.Master" %>
<asp:Content runat="server" ID="Content2" ContentPlaceHolderID="MainContent">
<% using (Html.BeginForm("MyAction", "MyController")) {%>
<%= Html.EditorFor(m => m.SomeString) %>
<%= Html.EditorFor(m => m.SomeDictionary["somevalue"]) %>
<input type="submit" value="Go" />
<%} %>
</asp:Content>
Run Code Online (Sandbox Code Playgroud)
作为参考,HTML输出是: …
t-sql ×2
asp.net ×1
asp.net-mvc ×1
c# ×1
cursor ×1
microkernel ×1
modelbinders ×1
ruby ×1
solr ×1
sql ×1
sql-server ×1
ssis ×1
xml ×1