1. sql server如何用print语句输出查询结果
1、可以使用如下程序将元组的多个属性输出
DECLARE @t1 NVARCHAR(100) --声明一个变量,接收查询结果值。
SELECT @t1=com_name FROM com WHERE cust_id='20100001' --查询
PRINT @t1 --输出结果值。
SELECT @t1=com_name FROM com WHERE cust_id='1405892'
PRINT @t1
SELECT @t1=com_name FROM com WHERE cust_id='569454'
PRINT @t1
SELECT @t1=com_name FROM com WHERE cust_id='647328'
PRINT @t1
SELECT @t1=com_name FROM com WHERE cust_id='1221889'
PRINT @t1
SELECT @t1=com_name FROM com WHERE cust_id='1255607'
PRINT @t1
2、--向上边的批量select查询,用print输出后,在消息中,还能查看结果。如果不用print,就需要一个一个的复制查询结果。
3、--上边的语句,是在excel和word中拼接和替换值,得到的批量查询语句。
(1)sql输出自然数扩展阅读:
1、不带输出项的print即为输出一个空行,如果之前的print语句输出项的最后用“,”或“;”,则表示其输出是在同一行上。其后面的空的print语句用来消除前面的print语句一直在同一行上输出的效果,使其后面的输出是在下一行。
Print()
功能
以当前字体在打开的打印作业中打印一行或多行文本。
语法Print(printjobnumber,{tab1,}string{,tab2})
例如用在编程中:
定义一个整型数组,将50个随机的两位正整数从下标1开始放入该数组中,求出该数组中具有偶数值的偶数下标元素之和,同时输出该数组中所有元素的值,每行输出10个值。
dim a(50) asinteger
dim i,s asinteger
randomize
s=0
for i=1 to 50
a(i)=int(rnd()*99)+1
if a(i) mod 2=0 then s=s+i
next i
print "s=";s
for i=1 to 50
print a(i);
if i mod 10=0 then print
next i
2、SQL中Print语句用于调试,所以,它输出的内容属于调试信息,类似于出错信息。
3、在不同的编程中,获取调试信息的,方法不同。此外,很少有人用Print作正常的输出,当然,在调试过程中用除外。要输出时,一般用Select语句来得方便一些。多组信息需要输出时,先生成一个临时表,然后向临时表添加,最后把总的临时表数据向前端推送即可。
2. t-sql写从1到500所有自然数中不含数字4的自然数
一个一个写 有些麻烦 1-500中 个位是4的有4,14,24,34,44,54,64,74,84,94……每个100中应该有10那么就有40个十位是4的有40,41,42,43,45,46,47,48,49,44也是10个,去掉44,那么500个数字中就有39个而百位上是4的有400——499共有100个,那么500-40-36-100=324个不知是否正确 望采纳!
3. sql 如何格式化输出这个数字
SELECT SUBSTRING(CONVERT(char, 320.01), 1, 1) + '"' + SUBSTRING(CONVERT(char, 320.01), 2, 2)
same result.
4. 使用t-sql语言实现输出1-100以内能够同时被3和5整除的整数
这种问题如果是特定针对的话,直接这么写就行了
declare@nint
select@n=1
while@n*15<100
begin
print@n*15;
select@n=@n+1;
end
如果有特定的输出格式要求,或者是要做通用的,就写存储过程,但是基本也是这么写。
5. 用T-SQL脚本程序编写程序,随机产生100个1到100之间的自然数,计算其累加和并输出。
DECLARE @Count int
DECLARE @Sum int
DECLARE @Num int
SELECT @Count=100,@Sum=0
WHILE (@Count>0)
BEGIN
SELECT @Num=CAST(RAND()*100 AS INT)
SELECT @Sum=@Sum+@Num,@Count=@Count-1
END
PRINT CONVERT(NVARCHAR(10),@Sum)
6. sql语句的自然数运算
--创建 number 表
create table number(num int)
--向表中插入[501,1000], [1501,2000]共1000个自然数
declare @i int, @p int
set @i = 501
set @p = 1001
while (@i < 2001)
begin
while (@i < @p)
begin
insert into number values (@i)
set @i = @i + 1
end
if (@i = 1001)
begin
set @i = 1501
set @p = 2001
end
end
--复制一张表,表结构和number表一样,表名为:table_3
select * into table_3 from number where 1 = 0
--把number表中所有是3的倍数的数字插入到新表table_3中并输出查看
insert into table_3
select *
from number
where num % 3 = 0
--复制一张表,表结构和number表一样,表名为:table_4
select * into table_4 from number where 1 = 0
--把number表中满足以下条件的数字找出来,插入到表table_4中
insert into table_4
select *
from number
where (num/1000 + num/100%10 + num/10%10 + num%10)%10 = 2
7. 用sql输入一个三位数,要求输出,个位,十位,百位
declare@threenumint
declare@ivarchar(1)
declare@jvarchar(1)
declare@kvarchar(1)
set@threenum=100--这个位置输入三位数
set@i=substring(CAST(@threenumasvarchar),1,1)
set@j=substring(CAST(@threenumasvarchar),2,1)
set@k=substring(CAST(@threenumasvarchar),3,1)
print('百位数为'+@i+','+'十位数为'+@j+','+'个位数为'+@k)
运行结果:
8. sql语句中如何将数值格式输出
select convert(decimal(38,2), AA.a )
from
(
select 1.1111 as a
union
select 1.2222 as a
union
select 1.3333 as a
union
select 1.5555 as a
) as AA
你这里的话用convert(decimal(38,2), sum(hf)/100 )as res_je 就可以了
decimal(38,2) 意思是38位的数字,其中2位是小数位
9. sql自动生成自然数列怎么写
select a.col*1000+b.col*100+c.col*10+d.col+1 as col
from
(select 0 as col union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9)a
cross join
(select 0 as col union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9)b
cross join
(select 0 as col union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9)c
cross join
(select 0 as col union all select 1 union all select 2 union all
select 3 union all select 4 union all select 5 union all
select 6 union all select 7 union all select 8 union all select 9)d
order by col
10. T-SQL脚本程序2. 编写程序,随机产生100个1到100之间的自然数,计算其累加和并输出.急。。。。。。。。
create proc test2
as
begin
declare @i int
declare @m int
declare @n int
declare @table1 table(id int identity,num int)
set @i=1
set @n=1
set @m=0
while @i<=100
begin
declare @j int
INSERT INTO @table1(num)
SELECT CAST(RAND()*100 AS INT)+1
set @i=@i+1
end
while @n<=100
begin
select @m=@m+num from @table1 where id=@n
set @n=@n+1
end
print @m
end
--运行:
test2