克隆现有记录并修改一个字段。周期

时间:2015-10-19 08:42:59

标签: sql sql-server-2008

我的问题如下:

具有多个记录,多个属性的SQL表。

让我们选择它:

select * from jaikudo

返回:

(id, created, createdByWho,relatedToWho,worksWithWho )

要完成的工作:

当我:select * from jaikudo where createdByWho = 1 返回1条记录。

我有作品与表约5000条记录。 Id是PK,不是随机升序或降序而是随机数

当我:select * from worksWithWho

返回(id,created,createdByWho,)

我需要根据以下内容将记录插入jaikudo表中

`select * from jaikudo where createdByWho = 1`

返回1条记录(id,created,createdByWho,relatedToWho,worksWithWho)

复制详细信息:创建的(createdByWho,relatedToWho)将获取getdate()信息并插入如下:

在jaikudo表中为每个worksWithWho数据(这是唯一的)创建新记录,并使用从以下查询复制的数据填充剩余字段(由Who和relatedToWho创建):select * from jaikudo where createdByWho = 1

当它完成时,我必须在jaikudo表中有大约5000个新记录,但如果可能的话,一些错误检测将是好的,因为如果jaikudo表已经具有完全相同的关系然后跳过记录并继续

感谢。

2 个答案:

答案 0 :(得分:0)

你可以试试这个:

DECLARE @max_worksWithWho INT = 1
    WHILE @max_worksWithWho <= 5000
    BEGIN
     INSERT INTO jaikudo
     SELECT id, created, createdByWho, relatedToWho, @max_worksWithWho
     FROM jaikudo
     WHERE createdByWho = 1
       AND worksWithWho  NOT IN (SELECT worksWithWho FROM jaikudo WHERE createdByWho = 1)
    SET @max_worksWithWho = @max_worksWithWho + 1
    END

答案 1 :(得分:0)

根据您更新的问题, 您可JOIN worksWithWho并执行此类操作。

INSERT INTO jaikudo(id, created, createdByWho,relatedToWho,worksWithWho)
SELECT
    CHECKSUM(NEWID()) as id,
    GETDATE() as created,
    j1.createdbyWho,
    j1.relatedToWho,
    w.worksWithWho
FROM worksWithWho w
INNER JOIN jaikudo j1
ON j1.createdByWho = 1
LEFT JOIN jaikudo j
ON j.worksWithWho = w.id
WHERE j.id IS NULL
    添加了
  1. INNER JOIN jaikudo j1 ON j1.createdByWho = 1以获取createdbyWhorelatedToWho
  2. 的详细信息 添加了
  3. LEFT JOIN jaikudo j ON j.worksWithWho = w.id WHERE j.id IS NULL,仅添加已存在相同worksWithWho的记录的记录
  4. 注意:使用w.id或任何列存储id worksWithWho表中worksWithWho的引用using System; using Windows.ApplicationModel.Background; using Windows.Foundation; using Windows.UI.Xaml; using Windows.Devices.Gpio; using System.Diagnostics; namespace StepperBackgroundV3 { public sealed class StartupTask : IBackgroundTask { private const int Stepper_PIN_A = 18; private const int Stepper_DIR_A = 27; private const int Sleep_PIN_A = 13; private const int Enable_PIN_A = 16; private const int Fault_PIN_A = 24; private GpioPin StepperDirA; private GpioPin StepperPinA; private GpioPin FaultPinA; private GpioPin SleepPinA; private GpioPin EnablePinA; private DispatcherTimer timer; private double BEAT_PACE = 1000; private double CounterClockwiseDanceMove = 1; private double ClockwiseDanceMove = 2; private double currentDirection; private double PulseFrequency = 20; Stopwatch stopwatch; public void Run(IBackgroundTaskInstance taskInstance) { //InitializeComponent(); // // !!! Wieder einbauen zum Starten der Motoren !!! // this.InitDancing(); // // //pausiereMotoren(); //erwarteAnweisungen(); } private void InitDancing() { try { var gpio = GpioController.GetDefault(); if (gpio == null) { StepperPinA = null; return; } StepperPinA = gpio.OpenPin(Stepper_PIN_A); StepperPinA.SetDriveMode(GpioPinDriveMode.Output); StepperDirA = gpio.OpenPin(Stepper_DIR_A); StepperDirA.SetDriveMode(GpioPinDriveMode.Output); FaultPinA = gpio.OpenPin(Fault_PIN_A); FaultPinA.SetDriveMode(GpioPinDriveMode.Input); SleepPinA = gpio.OpenPin(Sleep_PIN_A); SleepPinA.SetDriveMode(GpioPinDriveMode.Output); EnablePinA = gpio.OpenPin(Enable_PIN_A); EnablePinA.SetDriveMode(GpioPinDriveMode.Output); stopwatch = Stopwatch.StartNew(); currentDirection = 0; // Initially we aren't dancing at all. timer = new DispatcherTimer(); timer.Interval = TimeSpan.FromMilliseconds(BEAT_PACE); timer.Tick += Beat; if (StepperPinA != null) { timer.Start(); Windows.System.Threading.ThreadPool.RunAsync(this.MotorThread, Windows.System.Threading.WorkItemPriority.High); } }catch(Exception e) { } } private void Beat(object sender, object e) { if (currentDirection != ClockwiseDanceMove) { currentDirection = ClockwiseDanceMove; //GpioStatus.Text = "Yay!"; } else { currentDirection = CounterClockwiseDanceMove; //GpioStatus.Text = "Windows 10!"; } } private void MotorThread(IAsyncAction action) { while (true) { if (currentDirection != 0) { StepperPinA.Write(GpioPinValue.High); } // Könnten es noch heruntersetzen auf 0.1 ms Wait(currentDirection); StepperPinA.Write(GpioPinValue.Low); Wait(PulseFrequency - currentDirection); } } private void Wait(double milliseconds) { long initialTick = stopwatch.ElapsedTicks; long initialElapsed = stopwatch.ElapsedMilliseconds; double desiredTicks = milliseconds / 1000.0 * Stopwatch.Frequency; double finalTick = initialTick + desiredTicks; while (stopwatch.ElapsedTicks < finalTick) { } } //Standbymode private void pausiereMotoren() { //Sleep setzen.... } //Communicationssocket private void erwarteAnweisungen() { //Clientsocket Coden //Irgendwie den Start der Motoren aufrufen //this.InitDancing(); } //Fehlererkennung private void Fault() { if(FaultPinA.Read() == GpioPinValue.Low) { //LED und Summer ansteuern //String an Communicationlogger MotorA-Fehler pausiereMotoren(); } //if (FaultPinB.Read() == GpioPinValue.Low) //{ // //LED und Summer ansteuern // //String an Communicationlogger MotorB-Fehler // pausiereMotoren(); //} } } }