SQL Server Merge with case? - sql-server

Hello guys here with another question, regarding sql server 2008 r2 this time about the merge, is it possible to have a case inside the update portion of the merge?
Because its telling me
Msg 156, Level 15, State 1, Line 9
Incorrect syntax near the keyword 'CASE'.
MERGE INTO PERSONAFISICA AS TARGET
USING dbo.#temp1 AS SOURCE
ON TARGET.RFC = SOURCE.RFC AND TARGET.APATERNO = SOURCE.APELLIDO_PATERNO AND
TARGET.AMATERNO = SOURCE.Apellido_Materno
WHEN MATCHED THEN
UPDATE SET
TARGET.NUM_CLIENTE = 0,
TARGET.NOMBRE1 = LEFT(SOURCE.Nombre,CHARINDEX(' ', SOURCE.NOMBRE + ' ') -1),
CASE
WHEN LEN(SOURCE.NOMBRE) - LEN(REPLACE(SOURCE.NOMBRE,' ','')) >= 1
THEN
TARGET.NOMBRE2 = SUBSTRING(SOURCE.NOMBRE, CHARINDEX(' ', SOURCE.NOMBRE)+1, LEN(SOURCE.NOMBRE))
ELSE '' END,
TARGET.APATERNO = SOURCE.Apellido_Paterno,
TARGET.AMTERNO = SOURCE.Apellido_Materno,
CASE
WHEN SOURCE.SEXO = 'F'
THEN TARGET.IDGENERO = 2
WHEN SOURCE.SEXO = 'M'
THEN TARGET.IDGENERO = 1
ELSE TARGET.IDGENERO = 0
END,
CASE
WHEN SOURCE.ESTADO_CIVIL = '0'
THEN TARGET.idestado_civil = 0
WHEN SOURCE.ESTADO_CIVIL = 'C'
THEN TARGET.idestado_civil = 1
WHEN SOURCE.ESTADO_CIVIL = 'D'
THEN TARGET.idestado_civil = 2
WHEN SOURCE.ESTADO_CIVIL = 'S'
THEN TARGET.idestado_civil = 3
WHEN SOURCE.ESTADO_CIVIL = 'V'
THEN TARGET.idestado_civil = 5
WHEN SOURCE.ESTADO_CIVIL = 'U'
THEN TARGET.idestado_civil = 6
ELSE TARGET.idestado_civil = 0
END,
TARGET.idregimen = 0,
TARGET.saludo = SOURCE.SALUDO,
TARGET.conyuge_nombre1 = '',
TARGET.conyuge_nombre2 = '',
TARGET.conyuge_apaterno = '',
TARGET.conyuge_nombre2 = '',
TARGET.dependiente = 0,
TARGET.edad_dependiente = '',
TARGET.ididentificacion = 0,
TARGET.fecha_nacimiento = '1800-01-01',
TARGET.rfc = SOURCE.RFC,
TARGET.CURP = '',
TARGET.idnacionalidad = 0,
TARGET.email = SOURCE.Email_Personal,
TARGET.idescolaridad = SOURCE.Escolaridad
WHEN NOT MATCHED THEN
INSERT (num_cliente, nombre1, nombre2, apaterno, amaterno, idgenero, idestado_civil,
idregimen, saludo, conyuge_nombre1, conyuge_nombre2, conyuge_apaterno, conyuge_amaterno, dependiente,
edad_dependiente, ididentificacion, fecha_nacimiento, rfc, curp, idnacionalidad, email, idescolaridad)
VALUES (0, LEFT(nombre,CHARINDEX(' ',nombre + ' ')-1) AS [Primer Nombre],
CASE WHEN LEN(nombre) - LEN(REPLACE(nombre,' ','')) >= 1 THEN SUBSTRING(nombre, CHARINDEX(' ', nombre)+1, LEN(nombre)) ELSE '' END AS [Segundo Nombre],
Apellido_Paterno,Apellido_Materno)
CASE
WHEN SEXO = 'F' THEN 2
WHEN SEXO = 'M' THEN 1
ELSE 0
END,
CASE
WHEN Estado_Civil = '0' THEN 0
WHEN Estado_Civil = 'C' THEN 1
WHEN Estado_Civil = 'D' THEN 2
WHEN Estado_Civil = 'S' THEN 3
WHEN Estado_Civil = 'V' THEN 5
WHEN Estado_Civil = 'U' THEN 6
ELSE 0
END, 0, Saludo,'','','','',0,'',0,'1800-01-01',RFC,'',0,Email_Personal,Escolaridad

You can't use a case in that way in an update. It needs to be like ...
UPDATE table
SET value1 = CASE
WHEN a.blah = b.blah THEN foo
WHEN a.blah > b.blah THEN bar
ELSE NULL
END,
value2 = 5,
......

Related

Add picture to objects from array table in Lua

I am creating 8 images using Cheat Engine Lua script and I want to add some pictures to the images from an array table.
the_famous = {
{photo = "AlbertEinstein.jpg", nickName = "einstein", actorName = "Albert Einstein", profession = "Physicist", creation = "Theory of relativity", nationality = "Germany", life = "14 March 1879 - 18 April 1955"},
{photo = "Mozart.jpg", nickName = "mozart", actorName = "Wolfgang Amadeus Mozart", profession = "Classical Music Composer", creation = "Symphony No.40", nationality = "Austria", life = "27 January 1756 – 5 December 1791"},
{photo = "Guevara.jpg", nickName = "guevara", actorName = "Ernesto 'Che' Guevara", profession = "Revolutionary", creation = "Cuban Revolution", nationality = "Cuba, Argentina ", life = "14 June 1928 - 9 October 1967"},
{photo = "BruceLee.jpg", nickName = "bruce lee", actorName = "Bruce Lee (Lee Jun Fan)", profession = "Martial Artist", creation = "Father of MMA", nationality = "Hongkong, America", life = "27 November 1940 - 20 July 1973"},
{photo = "Marilyn.jpg", nickName = "marilyn monroe", actorName = "Marilyn Monroe (Norma Jeane Mortenson)", profession = "Artist", creation = "The most famous sex symbol", nationality = "America", life = "1 June 1926 - 5 August 1962"}}
function start()
if #the_famous ~= 0 then
the_famous.ItemIndex = 0
local idx = the_famous.ItemIndex
for i = 1, 8 do
local idx = tableTemp.ItemIndex
local img = string.format('image1'..i) -- images name start with 'image11' till 'image18'
local pic = the_famous[i].photo
img.Picture.loadFromFile(path_to_images_stored..tostring(pic))
img.Hint = the_famous[i].nickname
idx = idx + 1
end
end
end
But the function above doesn't work. How I can do it properly?
Solved
I have change my script:
local cleft = 20
local ctop = 20
function getCategory(sender)
local cat = sender.Name
if cat == 'btnFamous' then
while #tableTemp ~= 0 do rawset(tableTemp, #tableTemp, nil) end
tableTemp = {}
for z=1, #the_famous do tableTemp[z] = the_famous[z] end
else
return nil
end
for i = 1, 24 do
local newImg = createImage(mPanel)
newImg.width = 90
newImg.height = 90
newImg.top = ctop
newImg.left = cleft
newImg.Stretch = true
newImg.Picture.loadFromFile(famous_img..tostring(tableTemp[i].photo))
newImg.showHint = true
newImg.Hint = tostring(tableTemp[i].nickName)
newImg.Name = 'pic00'..i
cleft = cleft + 100
if i == 8 or i == 16 then
cleft = 20
ctop = ctop + 100
end
newImg.OnMouseEnter = function()
hglight.visible=true
hglight.setPosition(newImg.Left-10, newImg.Top-10)
end
newImg.OnMouseLeave = function()
hglight.visible=false
hglight.setPosition(newImg.Left-10, newImg.Top-10)
end
end
end
All works properly

Many Updates -> One Update?

Once a month we receive a file from another company and we need to adapt it to our database (in SQL Server). For this we run several updates that take a long time.
Is there any way to "convert" all these updates into a single statement so it runs through the table only once? (it's a really big table!).
We are using something like this right now:
update Table1 set column01 = 0 where column01 = ' ' or column01 = '';
update Table1 set column02 = 0 where column02 = ' ' or column02 = '';
update Table1 set column03 = 0 where column03 = ' ' or column03 = '';
update Table1 set column04 = 0 where column04 = ' ' or column04 = '';
update Table1 set column05 = 0 where column05 = ' ' or column05 = '';
update Table1 set column06 = 0 where column06 = ' ' or column06 = '';
update Table1 set column07 = 0 where column07 = ' ' or column07 = '';
update Table1 set column08 = 0 where column08 = ' ' or column08 = '';
update Table1 set column09 = 0 where column09 = ' ' or column09 = '';
update Table1 set column10 = 0 where column10 = ' ' or column10 = '';
update Table1 set column11 = 0 where column11 = ' ' or column11 = '';
update Table1 set column12 = 0 where column12 = ' ' or column12 = '';
update Table1 set column13 = 0 where column13 = ' ' or column13 = '';
update Table1 set column14 = 0 where column14 = ' ' or column14 = '';
update Table1 set column15 = 0 where column15 = ' ' or column15 = '';
update Table1 set column16 = 0 where column16 = ' ' or column16 = '';
update Table1 set column17 = 0 where column17 = ' ' or column17 = '';
update Table1 set column18 = 0 where column18 = ' ' or column18 = '';
update Table1 set column19 = 0 where column19 = ' ' or column19 = '';
You could use CASE ... WHEN like this
update Table1 set
column01 = CASE when column01 = ' ' or column01 = '' then 0 ELSE column01 END,
column02 = CASE when column02 = ' ' or column02 = '' then 0 ELSE column02 END,
......
column19 = CASE when column19 = ' ' or column19 = '' then 0 ELSE column19 END

LUA A function and an array, declared id, null ariable?

I have a problem with a function and an array since yesterday. It seems that lever ID is not declared or something...
Here is my code:
function tpp(leverID, from, to)
if item.uid == leverID and item.itemid == 1945 then
local count_players = #to
local store = {}
for i = 1, count_players do
local pid = getTopCreature(from[i]).uid
if (pid == 0 or not isPlayer(pid)) then
return doPlayerSendCancel(cid, 'You need ' .. count_players .. ' players to use this lever.')
end
store[i] = pid
end
for i = 1, count_players do
doSendMagicEffect(from[i], CONST_ME_POFF)
doTeleportThing(store[i], to[i], false)
doSendMagicEffect(to[i], CONST_ME_TELEPORT)
end
doTransformItem(item.uid, item.itemid + 1)
elseif item.uid == leverID and item.itemid == 1946 then
doTransformItem(item.uid, item.itemid -1)
end
end
function onUse(cid, item, fromPosition, itemEx, toPosition)
local pos = {
['pos_start'] = {
{['x'] = 1059, ['y'] = 1034, ['z'] = 7},
{['x'] = 1060, ['y'] = 1034, ['z'] = 7}
},
['pos_end'] = {
{['x'] = 1059, ['y'] = 1032, ['z'] = 7},
{['x'] = 1060, ['y'] = 1032, ['z'] = 7}
}
}
tpp(10150, pos['pos_start'], pos['pos_end'])
return true
end
I get the this error:
attempt to index global 'item' (a nil value)
I am new to lua. Can someone help me? Thanks!
Probably you forgot to pass 'item' variable to tpp() function when you called it from onUse() function.
Since there was no 'item variable in scope of tpp() function, or in its arguments, variable considered to be global, and there's no global variable with that name.

Excel bracket using VBA not working?

Making an autograde of sorts for an excel bracket with the guesses of each bracket in the ranges marked. Still clearly work in progress, finally have it running through, but the loop isn't working, it's returning pointSum of 0. Trying to sum += 10 when points is true.
Sub foo()
Dim Player(1 To 35) As String
Player(1) = Range("M2")
Player(2) = Range("M4")
Player(3) = Range("M10")
Player(4) = Range("M12")
Player(5) = Range("M22")
Player(6) = Range("M24")
Player(7) = Range("M32")
Player(8) = Range("M34")
Player(9) = Range("L1")
Player(10) = Range("L3")
Player(11) = Range("L5")
Player(12) = Range("L7")
Player(13) = Range("L9")
Player(14) = Range("L11")
Player(15) = Range("L13")
Player(16) = Range("L15")
Player(17) = Range("L20")
Player(18) = Range("L22")
Player(19) = Range("L24")
Player(20) = Range("L26")
Player(21) = Range("L28")
Player(22) = Range("L30")
Player(23) = Range("L32")
Player(24) = Range("L34")
Player(25) = Range("K2")
Player(26) = Range("K6")
Player(27) = Range("K10")
Player(28) = Range("K14")
Player(29) = Range("K21")
Player(30) = Range("K25")
Player(31) = Range("K29")
Player(32) = Range("J4")
Player(33) = Range("J12")
Player(34) = Range("J23")
Player(35) = Range("J31")
Dim Winner(1 To 18) As String
Winner(1) = "Mohler"
Winner(2) = "Scotter"
Winner(3) = "DKGAY"
Winner(4) = "Lassie"
Winner(5) = "Mohler"
Winner(6) = "Gunnar"
Winner(7) = "Gord'n"
Winner(8) = "Hellmers"
Winner(9) = "Evan Brown"
Winner(10) = "Jerru"
Winner(11) = "Case"
Winner(12) = "Lassie"
Winner(13) = "Mohler"
Winner(14) = ""
Winner(15) = ""
Winner(16) = "Mohler"
Winner(17) = "Mohler"
Winner(18) = ""
Dim Guess(1 To 18) As String
Guess(1) = Player(10)
Guess(2) = Player(14)
Guess(3) = Player(18)
Guess(4) = Player(23)
Guess(5) = Player(25)
Guess(6) = Player(26)
Guess(7) = Player(27)
Guess(8) = Player(28)
Guess(9) = Player(29)
Guess(10) = Player(30)
Guess(11) = Player(31)
Guess(12) = Player(32)
Guess(13) = ""
Guess(14) = ""
Guess(15) = ""
Guess(16) = ""
Guess(17) = ""
Guess(18) = ""
Dim points(1 To 18) As Boolean
points(1) = False
points(2) = False
points(3) = False
points(4) = False
points(5) = False
points(6) = False
points(7) = False
points(8) = False
points(9) = False
points(10) = False
points(11) = False
points(12) = False
points(13) = False
points(14) = False
points(15) = False
points(16) = False
points(17) = False
points(18) = False
Dim pointSum As Double
pointSum = 0
Dim pointValue(1 To 6) As Double
pointValue(1) = 10
pointValue(2) = 20
pointValue(3) = 30
pointValue(4) = 40
pointValue(5) = 50
pointValue(6) = 60
For i = 1 To 12 Step 1
If Guess(i) = Winner(i) Then
points(i) = True And pointSum = pointSum + 10
Else
points(i) = False
End If
Next
Range("O1") = pointSum
MsgBox "Done!"
End Sub
For i = 1 To 12 Step 1
If Guess(i) = Winner(i) Then
points(i) = True And pointSum = pointSum + 10
Else
points(i) = False
End If
Next
Ok, several things.
Step 1
That's the default; the Step part of the For loop definition is usually only included when its value is different than 1. But that won't cause any problems.
points(i) = True And pointSum = pointSum + 10
If I understand your question correctly...
the loop isn't working, it's returning pointSum of 0. Trying to sum += 10 when points is true.
You're not assigning pointSum anywhere. The above line of code is interpreted as follows:
points(i) = (True And (pointSum = pointSum + 10))
In other words:
points(i) = (True And False)
In other words:
points(i) = False
When you make an assignment, the variable being assigned to goes on the left side of the assignment operator (=):
foo = 42 'assigns value 42 to foo
The confusion seems to be that in VBA, the comparison operator is also a = token:
If foo = 42 Then 'true when the value of foo is 42
To increment pointSum by 10 when points(i) is True, you can do this:
If points(i) = True Then pointSum = pointSum + 10
Which can be simplified to:
If points(i) Then pointSum = pointSum + 10
Because the (boolean expression) in If (boolean expression) Then doesn't need to be compared to True or False when you're already working with a Boolean variable.
Hope it helps!

lights out game using CORONA SDK

am trying to devalope a lights out game with CORONA SDK
but am not able to figure out a way for looping it !!!
how many functions to create and the way to keep this going
here is my code (its dummy but a friend gave it to me as am trying to go on from there )
obj = nil
px = 35
py = 50
r = 22
xi = 60
yi = 60
x1y1 = display.newCircle(px+xi*0,py+yi*0,r) x1y1.id = "x1y1"
x2y1 = display.newCircle(px+xi*1,py+yi*0,r) x2y1.id = "x2y1"
x3y1 = display.newCircle(px+xi*2,py+yi*0,r) x3y1.id = "x3y1"
x4y1 = display.newCircle(px+xi*3,py+yi*0,r) x4y1.id = "x4y1"
x5y1 = display.newCircle(px+xi*4,py+yi*0,r) x5y1.id = "x5y1"
x1y2 = display.newCircle(px+xi*0,py+yi*1,r) x1y2.id = "x1y2"
x2y2 = display.newCircle(px+xi*1,py+yi*1,r) x2y2.id = "x2y2"
x3y2 = display.newCircle(px+xi*2,py+yi*1,r) x3y2.id = "x3y2"
x4y2 = display.newCircle(px+xi*3,py+yi*1,r) x4y2.id = "x4y2"
x5y2 = display.newCircle(px+xi*4,py+yi*1,r) x5y2.id = "x5y2"
x1y3 = display.newCircle(px+xi*0,py+yi*2,r) x1y3.id = "x1y3"
x2y3 = display.newCircle(px+xi*1,py+yi*2,r) x2y3.id = "x2y3"
x3y3 = display.newCircle(px+xi*2,py+yi*2,r) x3y3.id = "x3y3"
x4y3 = display.newCircle(px+xi*3,py+yi*2,r) x4y3.id = "x4y3"
x5y3 = display.newCircle(px+xi*4,py+yi*2,r) x5y3.id = "x5y3"
x1y4 = display.newCircle(px+xi*0,py+yi*3,r) x1y4.id = "x1y4"
x2y4 = display.newCircle(px+xi*1,py+yi*3,r) x2y4.id = "x2y4"
x3y4 = display.newCircle(px+xi*2,py+yi*3,r) x3y4.id = "x3y4"
x4y4 = display.newCircle(px+xi*3,py+yi*3,r) x4y4.id = "x4y4"
x5y4 = display.newCircle(px+xi*4,py+yi*3,r) x5y4.id = "x5y4"
x1y5 = display.newCircle(px+xi*0,py+yi*4,r) x1y5.id = "x1y5"
x2y5 = display.newCircle(px+xi*1,py+yi*4,r) x2y5.id = "x2y5"
x3y5 = display.newCircle(px+xi*2,py+yi*4,r) x3y5.id = "x3y5"
x4y5 = display.newCircle(px+xi*3,py+yi*4,r) x4y5.id = "x4y5"
x5y5 = display.newCircle(px+xi*4,py+yi*4,r) x5y5.id = "x5y5"
bb = {x1y1,x2y1,x3y1,x4y1,x5y1,x1y2,x2y2,x3y2,x4y2,x5y2,x1y3,x2y3,x3y3,x4y3,x5y3,x1y4,x2y4,x3y4,x4y4,x5y4,x1y5,x2y5,x3y5,x4y5,x5y5}
iClicked = 0
function click(e)
if(e.phase == "ended") then
--circleID = e.target.id
--whichCircle()
print(e.target.id)
obj = e.target
for u=1,25 do
if(obj==bb[u]) then
iClicked = u
end
end
if((iClicked-5) > 0 and (iClicked-5) < 26) then
bb[iClicked-5]:setFillColor(1,0,0)
end
if((iClicked-1) > 0 and (iClicked-1) < 26) then
bb[iClicked-1]:setFillColor(1,0,0)
end
obj:setFillColor(1,0,0)
if((iClicked+1) > 0 and (iClicked+1) < 26) then
bb[iClicked+1]:setFillColor(1,0,0)
end
if((iClicked+5) > 0 and (iClicked+5) < 26) then
bb[iClicked+5]:setFillColor(1,0,0)
end
end
end
for k=1,25 do
bb[k]:addEventListener("touch",click)
end
its all about having 25 circles and lighting them on and off but it doesnt seem to work for me
any good help will be great
Thanks
local myCircles = {}
for y = 1, 5 do
myCircles[y] = {}
for x = 1, 5 do
myCircles[y][x] = display.newCircle(px+xi*0,py+yi*4,r)
myCircles[y][x].id = .id = "x" .. x .. "y" .. y
end
end
or something like that.
Rob

Resources