わかったようでわっかってない

1バイト=8ビット
ビットは 0 or 1
半角文字は8ビットで表せるので通称1バイト文字
全角文字は通称2バイト文字

なので半角数字で最大バイト数1
は8ビットだから最大の数は
11111111
2進法なので10進法に換算すると
1×27+1×26+1×25+1×24+1×23+1×22+1×21+1×20
=128+64+32+16+8+4+2+1=255
まじめに書けばこうなるが
11111111の次の数は100000000
十進法では28だから1引いて256-1=255 でもある

そこを踏まえてSQLserverでtinyintは1byteだから
0~255
ということになる

Accessパススルークエリ

VBAからクエリの発行は文字列使ってダイレクトなんとかですので
パススルークエリはほとんど使わないのですがAccessレポートの
ソースにsqlserverのテーブルにsql関数など入ったクエリをソースにする必要があり久しぶりに作成。
しかし
パススルークエリはレポートのソースに使えませんと警告

パススルークエリをソースに全選択のAccessクエリを作成してソースにすれば美しくはないですがなんとか可能になります。

接続文字列はどうすれば?と思いましたがプロパティでビルドボタン押してODBC選択すれば自動で入るのですね。

if exists

mysqlでもsqlserverでも
訂正 SQLserverのみ
MySQLはテーブルがあらかじめないとダメでした。

drop table if exists Table
select * into Table from otherT where ・・・

は便利と思うのですが同じことをAccessのローカルテーブルにしようと
VBAでcurrentproject.connection に
cn.execute “drop table if exists Table” とやっても通用しません

しばらく悩みましたが
on error resume next
docmd.setwarnings false
cn.execute “drop table Table”
docmd.setwarnings true
でとりあえずはなんとか

sqlserverの和暦

VBAでは和暦は出てくるけどsqlserverは日本の暦は表示できません
年月日ならば
FORMAT ( @d, ‘D’)で表示されるが実はこれ for china?

ということでH29/12/17のように変換するスカラー関数
CREATE FUNCTION iswareki
(
@paramDate datetime
)
RETURNS nvarchar(10)

AS
BEGIN
— Declare the return variable here
DECLARE @ResultVar nvarchar(10),@VarYear int,@VarDate nvarchar(10),@VarDateInt int

select @VarYear=cast(format(@paramDate,’yyyy’) as int)
select @VarDate=format(@paramDate,’MM/dd’)
select @VarDateInt=cast(format(@paramDate,’yyyyMMdd’) as int)

— Add the T-SQL statements to compute the return value here
select @ResultVar=(case
when @VarDateInt>19890107 then
concat(‘H’,format((@varyear-1988),’00’),’/’,@VarDate)–平成
when @VarDateInt>19261224 then
concat(‘S’,format((@varyear-1925),’00’),’/’,@VarDate) –昭和
when @VarDateInt>19120729 then
concat(‘T’,format((@varyear-1911),’00’),’/’,@VarDate) –大正
when @VarDateInt>18680124 then
concat(‘M’,format((@varyear-1867),’00’),’/’,@VarDate) –明治
else ‘0’
end
)
— Return the result of the function
RETURN @ResultVar

END
GO

一括insertできない件

失敗の原因はトリガーでした。
トリガー内の from inserted のinsertedを
レコードと思っていたのが原因です。
よく考えればfromだから次はテーブルですね。
さらにnull値更新のコード簡素化のために関数作ってパラメータに
from insertedを入れ込んだのでますますいけませんでした。
関数をやめて長々とコード書いて最後に
where no in (select no from inserted)
で解決
inserted deleted はテーブルと肝に銘じよう

DMLトリガーに関数、パラメータにfrom Insertedは

rebootで書いた 一括insertがうまくいかない件は
https://docs.microsoft.com/ja-jp/sql/relational-databases/triggers/create-dml-triggers-to-handle-multiple-rows-of-data
これかな?
insertトリガーに仕込んだ関数のパラメータにselect from insertedと書いてるからと思う。
と言うか違いない。

VBA二次元配列

Public Function theSuccessTest()
Dim Str As String
Str = “a,b,c,d,e,f,g”

Dim Ary() As String
Ary = Split(Str, “,”)

Debug.Print UBound(Ary, 1)

Dim Ary2() As String
ReDim Ary2(0, 3)
Ary2(0, 0) = “a”
Ary2(0, 1) = “b”
Ary2(0, 2) = “c”
ReDim Preserve Ary2(1, 3)
Ary2(1, 0) = “d”
Ary2(1, 1) = “e”
Ary2(1, 2) = “f”

Debug.Print UBound(Ary2, 1)
Debug.Print UBound(Ary2, 2) 
End Function

こういう風に今までやってきたのだが検索すると
二次元の順番が逆?? 混乱

Public Function theFailureTest()

Dim Str As String
Str = “a,b,c,d,e,f,g”

Dim Ary() As String
Ary = Split(Str, “,”)

Debug.Print UBound(Ary, 1)

Dim Ary2() As String
ReDim Ary2(3, 0)
Ary2(0, 0) = “a”
Ary2(1, 0) = “b”
Ary2(2, 0) = “c”
ReDim Preserve Ary2(3, 1)
Ary2(0, 1) = “d”
Ary2(1, 1) = “e”
Ary2(2, 1) = “f”

Debug.Print UBound(Ary2, 1)
Debug.Print UBound(Ary2, 2)
End Function

いやいやこっちはなぜかできない
イメージはExcelの経時表
Accessのテーブルをイメージしたらだめ・・なのか?

reboot

開発中のDBアプリ、システム復元で3ヶ月分戻ってしまった。
バックアップしてるつもりがしてなかった
一ヶ月ほど呆然としていたがリブートです。

sqlserverでselectした結果をinsertですが
トリガーでカラムに関数など仕込んでいるとうまくいかない
トリガーのwhere条件を 
where id=(select id from inserted)
から
where id in(select id from inserted)
にすればよいのだが
ほかがうまくいかない

wordpressは設定で日本語にすれば簡単に日本語にできるのですね
今日気がつきました。